Thank you both,
Firstly, what is the scope of a struct which is created (not as a
pointer) in a member function? I always thought it was local only
(would delete when the function is returned), but I'm clearly wrong
here. Does it last until the object that the member function belongs to
is destroyed?
A struct doesn't have a scope - it doesn't exist. An instance of a
struct does.
The local variable dies with the function but if you return the
variable, a copy ctor creates a brand new object in main for you.
Secondly, am I correct in thinking that there is no "inline" way of
creating a pointer to a struct like I was trying to, unless I define a
constructor? (I'm very stubborn, even if it isn't the right way, I like
to know where I was going wrong with the syntax).
Thanks again,
Matt
Look at it this way. If you *could* use a pointer, you'ld be much
better off using a const reference instead. Pointers are responsible
for most of the bugs that programs suffer from. Even in the case of
allocations, pointers should be replaced by smart_ptr(s) and references
work nicely with polymorphism.
A C++ programmer'ss ultimate goal is (or should be) to write an entire
application without ever directly using a pointer. The resulting code
will be safer, bug-proof, easily maintainable and effortlessly
expandable/extendable.
The solution: a const reference. The only viable alternative is return
by copy. Are you ready? Cause there is a moral to the story...
#include <iostream>
class Position {
int x, y;
public:
Position() : x(0), y(0)
{
std::cout << "Position()\n";
}
Position(int x_, int y_) : x(x_), y(y_)
{
std::cout << "Position(int, int)\n";
}
Position(const Position& copy)
{
std::cout << "copy Position\n";
x = copy.x;
y = copy.y;
}
int getx() const { return x; }
int gety() const { return y; }
};
class Graphic
{
Position pos; // precious coordinate
public:
Graphic() : pos() { }
void move(const Position& r_pos)
{
pos = r_pos;
}
const Position& getpos() const // by const ref
{
return pos;
}
};
int main()
{
Graphic graphic;
graphic.move( Position(10,10) );
Position gpos = graphic.getpos();
std::cout << "pos.x = " << gpos.getx();
std::cout << "\npos.y = " << gpos.gety();
std::cout << std::endl;
}
/*
Position() // 0,0
Position(int, int) // 10,10
copy Position // the reference is copied safely
pos.x = 10
pos.y = 10
*/
Graphic's getpos() above is a viable candidate for a pointer return
since its returning an internal member. Thing is: why would i want a
user of the class to have *access* to my precious graphic's Position?
Even if it were to be accessed + modified by accident? You could return
a constant pointer - but then some smart-ass can const_cast<> your
precious position into oblivion. I'm not joking - i'm dead serious.
Consider:
Position* const Graphic::getpos() // by constant pointer
{
return &pos;
}
some smart ass could write the following side-effect:
*graphic.getpos() = Position(-1,-1); // modifies your private member
!!!
and screw you royally. Try it. Even if getpos() is made const, i can
*still* modify the internal position with a Postion ptr and a
const_cast<>.
Forget doing that with a const reference. Its absolutely nuke-proof.