|
you have to provide a buffer that's big enough to hold the data from RegGetValue. an uninitialized pointer isn't enough.
|
|
|
|
|
tried
char achData[MAX_VALUE_NAME];
But still only one character!
|
|
|
|
|
How about using TCHAR instead of char. Sounds like your program is being compiled using unicode.
TCHAR achData[MAX_VALUE_NAME];
Kelly Herald
Software Developer
|
|
|
|
|
Hi, its been a while since I've posted here .
I've come back to C++ after doing quite a lot of scripting in Python though Scripting is still a passion of mine but enough about scripting.
I read an article about Memory Management in C++ the other day and some one wrote the code about borrowing a object through a reference even though it was created on the free store.
I'm mixed up about the various syntaxes heres some examples of code im mixed up with.
Robot.hpp
#include <iostream>
class Robot{
public: void Speak(){std::cout << "Hello\n";}
}
main.cpp
#include <iostream>
#include "Robot.hpp"
void RobotSpeak(Robot& robot);
void RobotSpeak2(Robot*& robot);
void RobotSpeak3(Robot** robot);
void RobotSpeak4(Robot* robot);
int main(){ return 0; }
When and how would you use RobotSpeak(Robot&) on a object created on the free store and when would you use the others.
Many Thanks in Advance.
Tom
modified on Saturday, May 8, 2010 9:55 AM
|
|
|
|
|
Soo….something like:
Robot* myRobot = new Robot;
RobotSpeak(*myRobot); ?
Oh, and BTW - the calling syntax for RobotSpeak3 would be (*robot)->Speak()
The only parameter passing mechanisms I'd consider using would be those used in RobotSpeak and RobotSpeak4…although I would tend to use a const reference and declare Speak as const if it doesn't modify Robot:
Robot.hpp
#include <iostream>
class Robot{
public: void Speak() const {std::cout << "Hello\n";}
};
main.cpp
void RobotSpeak(Robot const & robot)
{
robot.Speak();
}
int main()
{
Robot* myRobot = new Robot;
RobotSpeak(*myRobot);
return 0;
}
HTH!
Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p
CodeProject MVP for 2010 - who'd'a thunk it!
|
|
|
|
|
Thanks for replying .
Whats the difference between passing a object
by derefencing a pointed object and using
a using a Reference To Pointer, and a Pointer Argument,
like RobotSpeak(Robot const & robot) and RobotSpeak(Robot const *& robot ) and also RobotSpeak(Robot const ** robot) .
Tom
|
|
|
|
|
The only time I would use a reference to a pointer or a pointer to a pointer would be so I could modify the passed in pointer. And then, you need to be sure that you don't leak any memory.
Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p
CodeProject MVP for 2010 - who'd'a thunk it!
|
|
|
|
|
If you refer to the object by reference, it cannot by deleted, which is a good thing.
Where as if you did such as RobotSpeak(const Robot*& const robot) . You could delete the object in the procedure, and if the calling program tried to do any thing to object, it would fail and leak memory or worse.
Thanks Again
Tom
|
|
|
|
|
Tom Moore wrote: Where as if you did such as RobotSpeak(const Robot*& const robot). You could delete the object in the procedure, and if the calling program tried to do any thing to object, it would fail and leak memory or worse.
That's a very dangerous thing to do, and certainly not advisable in any API design. While you can create any level of parameter referencing you want (pointer to pointer to pointer to ...) you really have to consider why you would want to do such a thing, and what side effects you might be leaving yourself open to if you do.
Even if you explicitly state things like this when describing your API, users will always do incorrect things with it. Believe me, I've written what I thought were bomb-proof systems, only to find they weren't
Days spent at sea are not deducted from one's alloted span - Phoenician proverb
|
|
|
|
|
Thanks, I'll be aware of that in the future.
'Bomb-proof', reminds me of the old Atari ST,
whenever that crashed for whatever reason, it
would print cherry bombs on the screen, we calling
bombing out. That was early to mid 90's.
Thanks Again.
Tom
|
|
|
|
|
Not with that signature - you've got a const pointer to a const object - can't call delete on that without casting the pointer...
But even with a reference, you can delete it, like in this (evil, evil) code:
void RobotSpeak(const Robot& robot)
{
Robot* pRobot = const_cast<Robot*>(&robot);
delete pRobot;
}
Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p
CodeProject MVP for 2010 - who'd'a thunk it!
|
|
|
|
|
Thanks, didn't realize that could be done.
|
|
|
|
|
I would use
void RobotSpeak( const Robot & robot);
with
class Robot
{
public:
void Speak() const
{
std::cout << "Hello\n";
}
};
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
-- Iain Clarke
[My articles]
|
|
|
|
|
I really don't see the point in the standalone RobotSpeak function set. If you have an instance of Robot, say robot, why not just do:
robot.Speak();
And if you have different types of robots saying different things, make Speak virtual and operate through base classes.
The wonderful thing about the Darwin Awards is that everyone wins, especially the members of the audience.
|
|
|
|
|
Tim Craig wrote: I really don't see the point in the standalone RobotSpeak function set. If you have an instance of Robot, say robot, why not just do:
robot.Speak();
And if you have different types of robots saying different things, make Speak virtual and operate through base classes.
I know, but the point of the question wasn't about the Robot speaking, I was asking about using dynamically created objects through references.
|
|
|
|
|
Hi all,
i m using excel automation to read and write excel file.
i want fetch password of excel file.
please tell me how can i fetch password of excel file.
thanks in advance.
|
|
|
|
|
Fetch the password? You supply the password to Excel, you don't (can't?) retrieve it directly...
Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p
CodeProject MVP for 2010 - who'd'a thunk it!
|
|
|
|
|
|
Hi all,
In a dialog,im having a button.If i move my mouse over that button,I need to open a file.How to find out wther the mouse is over a button.I subclass that CButton class and captured that mouse action over a button using OnMouseMove function.But how can i send this response to parent dialog? Can anyone please help me?
Thanks,
|
|
|
|
|
you could:
1. send a message to the parent CWnd
or
2. pass a pointer to the parent to the button on creation, then call a public member on the parent
|
|
|
|
|
|
How to edit, add, delete these values found in Tools->VC++ Directories?
Чесноков
|
|
|
|
|
|
I meant to add new one e.g. $(MyOwnPath) or change path for available variables e.g. $(ProgramFiles) points to c:\program files to change the path value say to d:\program files
I need to find where these $ values are defined in VS
Чесноков
|
|
|
|
|
Chesnokov Yuriy wrote: I need to find where these $ values are defined in VS
See if this helps.
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"Man who follows car will be exhausted." - Confucius
|
|
|
|