Jump to content
  • Advertisement
Sign in to follow this  

default variable initialization in C++

This topic is 3302 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

In C# you initialize class bool and int members at declaration. In C++ is the constructor the means to do it?

Share this post


Link to post
Share on other sites
Advertisement
What is the syntax layout to declare and define a descendant class constructor which calls the ascendant constructor

class gm
[
gm();
];

gm::gm()
[

]

class gmhair : gm
[
gmhair();
];

gmhair:: gmhair() : gm()
[

]




[Edited by - Calin on May 2, 2009 2:13:39 AM]

Share this post


Link to post
Share on other sites
This is the syntax (you use the initialization list that rip-off mentioned)


class A
{
public:
// constructor
A();
};

A::A()
{
}

class B : public A
{
public:
B();
};

B::B() : A()
{
}

Share this post


Link to post
Share on other sites
ok, I'll try that. I have arguments in the constructors, the errors come from there I think

[Edited by - Calin on May 2, 2009 5:49:35 AM]

Share this post


Link to post
Share on other sites
It works fine with arguments, too. You just have to make them line up, i.e. call one of the base's actual constructors from the derived constructor initialization list.


class Base {
int x;
public:
Base(int x) : x(x) {}
};

class Derived: public Base {
int y:
public:
Derived(int x, int y): Base(x), y(y) {}
};


Here, 'x' is forwarded to the Base constructor, where it is used to initialize the 'x' member of the Base part of the derived instance. 'y' is used directly.

Share this post


Link to post
Share on other sites
Quote:
Original post by Zahlman
It works fine with arguments, too. You just have to make them line up, i.e. call one of the base's actual constructors from the derived constructor initialization list.


class Base {
int x;
public:
Base(int x) : x(x) {}
};

class Derived: public Base {
int y:
public:
Derived(int x, int y): Base(x), y(y) {}
};


Here, 'x' is forwarded to the Base constructor, where it is used to initialize the 'x' member of the Base part of the derived instance. 'y' is used directly.

thx,
what happens with class variables at instantiation when a) there isnt a constructor declared b) there is or there is not a constructor declared and instantiation takes place on the heap d) there are more than one constructor declared

[Edited by - Calin on May 2, 2009 12:02:30 PM]

Share this post


Link to post
Share on other sites
Quote:
Original post by Calin
what happens with class variables at instantiation when a) there isnt a constructor declared


All classes have at least one constructor. If you don't declare any, the "default constructor" is generated. It takes zero arguments, and simply calls zero-argument constructors for any members and base classes. If those members or base classes don't have a zero-argument constructor, then the compiler reports an error.

Quote:
b) there is or there is not a constructor declared and instantiation takes place on the heap


Allocation has nothing to do with construction. All the same things happen, just within the allocated chunk of the heap, instead of on the top of the stack.

Quote:
d) there are more than one constructor declared


If you have any constructor declared, then a default constructor is not generated. If you want a specific constructor to be used for initialization, you need to call it in the initialization list. (The compiler will figure out which one you want according to the arguments you provide.) Otherwise, the zero-argument constructor is used, if it exists; otherwise the compiler reports an error, as before.

There can, of course, be only one zero-argument constructor for a class, because the default one is not generated if you define one, and you can't define two (because it would really be two different definitions of the same constructor, and the compiler would report an error for that, too).

Share this post


Link to post
Share on other sites
I was the engine I was using that was giving me troubles I think, I was building an object inheriting from an engine base class. Zahlman thanks for taking time to explain things.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!