Jump to content
  • Advertisement
Sign in to follow this  
xissburg

[solved] calling a constructor inside other constructor C++

This topic is 4014 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

I want to know how to do in C++ a thing I usually do in Java, if possible :P. In Java I usually call a constructor inside another constructor for example:
public class Vector2
{
    public float x, y;

    public Vector2(float _x, float _y)
    {
        x = _x; y = _y;
    }

    public Vector2()
    {
        //calls the above constructor setting both x and y to 0.f
        this(0.f, 0.f);
    }

    //another example
    public Vector2(float s)
    {
        this(s, s);
    }
}




How to do it in C++? And how can I call a constructor of a base class A inside the constructor of a class B derived from A?! Something like this in Java:
public class Thing
{
    protected Vector2 position;

    public Thing()
    {
        this(0.f, 0.f);
    }

    public Thing(float x, float y)
    {
        position = new Vector2(x, y);
    }
}

public class AnotherThing extends Thing
{
    protected Vector2 velocity;

    public AnotherThing()
    {
        this(0.f, 0.f, 0.f, 0.f);
    }

    public AnotherThing(float vx, float vy)
    {
        this(0.f, 0.f, vx, vy);
    }

    public AnotherThing(float x, float y, float vx, float vy)
    {
        //calls a constructor of the base class
        super(x, y);
        velocity = new Vector2(vx, vy);
    }
}




I also want to know if this is a bad programming practice =/. Give me your advice. Many thanks :) [Edited by - xissburg on September 27, 2007 8:11:30 PM]

Share this post


Link to post
Share on other sites
Advertisement
You do it in the initialisation list like:


class base
{
public:
base(int, int);
};

class derived : public base
{
public:
derived(int);
};

derived::derived(int x) : base(x, 5)
{
}

Hope this helps,

Dan

Share this post


Link to post
Share on other sites
Quote:

You do it in the initialisation list like:

This is not what the OP is asking for. You're example is one of calling the base class constructor and the OP is asking about calling a sibling constructor (or constructor delegation), e.g., calling a constructor of the class type (not of any base type) in order to delegate some or all of the construction activity to that constructor.

C++ has no natural mechanism for this, however. The closest you can get is by emulating it via a private "initialization" function (although at this point you're no longer technically initializing the members, but assigning to them -- this is generally a nonissue though).

Share this post


Link to post
Share on other sites
Quote:
Original post by jpetrie
Quote:

You do it in the initialisation list like:


This is not what the OP is asking for. You're example is one of calling the base class constructor and the OP is asking about calling a sibling constructor (or constructor delegation), e.g., calling a constructor of the class type (not of any base type) in order to delegate some or all of the construction activity to that constructor.


Sorry but he answered my 2nd question :), thanks. What do you mean by 'OP'?! And thanks for the idea about the private initialization function. Well,its solved.

Thanks thanks . . . . .... . .... . .... . . . .... .. .. .. .. .. . . .. .. .. .

Share this post


Link to post
Share on other sites
I believe MrCheese was answering the OP's second question.

As for a way to do the first part, you can achieve a very similar effect with default parameters. For example:


class Vector2
{
float x,y;

Vector2(float x = 0.0f, float y = 0.0f)
{
this.x = x; this.y = y;
}
}





Share this post


Link to post
Share on other sites
Quote:
Original post by Muse
I believe MrCheese was answering the OP's second question.

As for a way to do the first part, you can achieve a very similar effect with default parameters. For example:

*** Source Snippet Removed ***


Similar effect that, in some cases, isn't suitable. You may want 0 or 2 arguments, but not only 1, as it could happen in your example. If you need variables to be initialized, you must type code twice, unfortunately.

Share this post


Link to post
Share on other sites
Thanks for the help and advices, I really appreciate it. I'm back here with another question somewhat similar to the previous ones.

I have a virtual (not pure) method in the base class and I override it in the derived class. But I want to 'append' code to the end of the method of the base class in the derived. Then, I need to call the method of the base inside the method of the derived. What is the best approach to do this? in Java I just put 'super.' before the name of the function and it calls the base method. How could it be coded in C++?

I had a crazy idea: to static_cast the this pointer(dereferenced) to the type of the base and call the function. It works but I dont know if there's something bad running behind the screen. What do you think?!

Thanks again :)

Share this post


Link to post
Share on other sites

struct Base {
virtual void function(void) {
std::cout << "Base::function()" << std::endl;
}
virtual ~Base() {}
};

struct Derived : Base {
virtual void function(void) {
Base::function();
std::cout << "Derived::function()" << std::endl;
}
};

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.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!