C++ Pointer Help [Solved]
-
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); // Calls robot.Speak()
void RobotSpeak2(Robot*& robot); // Calls robot->Speak()
void RobotSpeak3(Robot** robot); // I imagine robot->Speak()
void RobotSpeak4(Robot* robot); // I imagine robot->Speak() againint 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. Tommodified on Saturday, May 8, 2010 9:55 AM
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] -
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";} // Use 'const' because the method doesn't alter the Robot instance
};main.cpp
void RobotSpeak(Robot const & robot) // use a const reference to Robot to indicate the function doesn't modify 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)
andRobotSpeak(Robot const *& robot
) and alsoRobotSpeak(Robot const ** robot)
. Tom -
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)
andRobotSpeak(Robot const *& robot
) and alsoRobotSpeak(Robot const ** robot)
. TomThe 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!
-
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 ;P Tom -
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 ;P TomTom 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
-
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
-
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); // Calls robot.Speak()
void RobotSpeak2(Robot*& robot); // Calls robot->Speak()
void RobotSpeak3(Robot** robot); // I imagine robot->Speak()
void RobotSpeak4(Robot* robot); // I imagine robot->Speak() againint 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. Tommodified on Saturday, May 8, 2010 9:55 AM
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.
-
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 ;P TomNot 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);
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!
-
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.
-
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);
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!