🔒 Locked
Inheritance and constructors
Advertisement
Original Post
Ok I need to pass parameters to a class constructor through its subclass constructor. How the HECK do I do that. I''ve been all over my C++ book and it doesn''t say.
Basically I have this:
class object
{
object( int x, int y, int z);
};
class door : object
{
door( int x, int y, int z, int height, int width );
}
And I need door to send the xyz to object... ew?
Or should I just do the same code from object, in doors constructor? I think the default object constructor will override this though, when it sets everything to zero
Thanks!
class door : object
{
door( int x, int y, int z, int height, int width ) : object( x, y, z )
{;}
}
"To assert that the earth revolves around the sun is as erroneous as to claim that Jesus was not born of a virgin."
-- Cardinal Bellarmine
''send over'' ?
Ok I got that working, now I have a new question!
When I try to access a public member of the base class through the subclass, the compiler says no!
I get:
cannot access public member declared in class 'Object'
when I try:
door d;
int z = d->x;
Am I losing my mind? Its public right?
[edited by - aargyle on April 29, 2003 1:10:42 PM]
When I try to access a public member of the base class through the subclass, the compiler says no!
I get:
cannot access public member declared in class 'Object'
when I try:
door d;
int z = d->x;
Am I losing my mind? Its public right?
[edited by - aargyle on April 29, 2003 1:10:42 PM]
quote:
Original post by rypyr
door::door(int x, int y, int z, int height, int width) : object(x,y,z) {
// set height and width here...
}
Just wondering what''s the difference between door::door(params) : object(x, y, z) { } and door::door(params) { object(x, y, z); }
I use the second method as it''s clearer for me to read but is there some advantage to using : before the opening brackets?
quote:
Original post by Aargyle
Ok I got that working, now I have a new question!
When I try to access a public member of the base class through the subclass, the compiler says no!
I get:
cannot access public member declared in class ''Object''
when I try door->x
Am I losing my mind? Its public right?
class object
{
public:
int x,y,z;
object(int _x,int _y,int _z) : x(_x),y(_y),z(_z)
{}
};
class door : public Object
{
public: int width,height;
door( int x, int y, int z, int height, int width ) : object( x, y, z )
{//put initialization of width and height here
}
};
the keyword public ? you haven''t specified its !!!
Good luck !
kyo: i dun think u can call object(x,y,z) in the body of the constructor coz that would mean construction has already taken place. the idea of using the initializer list is to initialized values BEFORE the body of the function is called. in fact, if i'm not mistaken, the base class must be initialized before the child class.. which implies that the base class constructor can only be called in the initializer list.
[edited by - crazee on April 29, 2003 1:20:49 PM]
[edited by - crazee on April 29, 2003 1:20:49 PM]
Ah ok, I need the public in
class door : public Object
Thanks much! This is making my head swim, lol. I knew all this 3 years ago when I took my C++ class, and now when I come back to it its all gone! Mwahaha.
I do remember calling constructors in the body of a constructor though. Like
door::door(int x, int y, int z, int height, int width)
{
Object(x,y,z);
...
...
}
But I don''t know if its correct!
class door : public Object
Thanks much! This is making my head swim, lol. I knew all this 3 years ago when I took my C++ class, and now when I come back to it its all gone! Mwahaha.
I do remember calling constructors in the body of a constructor though. Like
door::door(int x, int y, int z, int height, int width)
{
Object(x,y,z);
...
...
}
But I don''t know if its correct!
quote:
Original post by Kyo
Just wondering what''s the difference between door::door(params) : object(x, y, z) { } and door::door(params) { object(x, y, z); }
I use the second method as it''s clearer for me to read but is there some advantage to using : before the opening brackets?
Yes, the advantage is it will work
Calling Object(x,y,z) in the constructor means you are creating a temporary Object that is discarded at the end of the Door constructor. Have you ever verified your base members after doing it? What you''ll notice is that your base members will have whatever values your default constructor initializes them to and x,y,z will not have been set correctly.
You should also always use initializer lists for member variables where possible and just get used to the syntax.
Unfortunately, I think Java has a slightly different syntax where you use super() to call the constructor explicitly so it can potentially get confusing.
Regards,
Jeff
Topic Locked
This topic has been locked by a moderator. New replies are not allowed.
Sign in to reply to this topic.
Advertisement