简体   繁体   中英

how to call parameterized and non parameterized constructor from single object in c++

If object is instance of whole class, why didn't we can call parameterized and non parameterized constructor from single object.

class ovrldng {
public:
    int var;
    ovrldng() {
        var=101;
        cout << "Default constructor assigned value into var=" << var ;
    }

    ovrldng(int x) {
        var=x;
        cout << "Parametrized assign 'x' value to var =" << var ;
    }
};

from main how to call both constructors from one object;

I would suggest you to use the calling constructor (with parameter) in constructor (without parameter), also known as constructor delegation:

ovrldng() : ovrldng(101)
{
    cout << "Default constructor assigned value into var=" << var ;
}

ovrldng(int x) : var(x)
{
    cout << "Parametrized assign 'x' value to var =" << var ;
}

and in main you can call both constructor just creating one object:

int main()
{
    ovrldng obj;
}

Hope that this what you are searching for.

There are multiple ways to initialize an object through multiple constructors.

Which one to use it is up to personal preferences/coding guidelines, use case (maybe the constructors are to different to share code) and technical limitations (eg the compiler doesn't support C++11 features):

#include <iostream>

//Your current solution (works pre C++11)
class ovrldng_1 {
public:
    int var;
    ovrldng_1() {
        var = 101;
        std::cout << "ovrldng_1 var =" << var << std::endl;
    }

    ovrldng_1(int x) {
        var = x;
        std::cout << "ovrldng_1 var =" << var << std::endl;
    }
};


//Using a function to initialize (works pre C++11)
class ovrldng_2 {
public:
    int var;
    ovrldng_2() {
        init(101);
    }

    ovrldng_2(int x) {
        init(x);
    }

    void init(int x){
        var = x;
        std::cout << "ovrldng_2 var =" << var << std::endl;
    }
};


//Using a default value (works pre C++11)
class ovrldng_3 {
public:
    int var;
    ovrldng_3(int x=101) {
        var = x;
        std::cout << "ovrldng_3 var =" << var << std::endl;
    }
};


//Calling one constructor from another (constructor delegation) (C++11 is required)
class ovrldng_4 {
public:
    int var;
    ovrldng_4(int x) {
        var = x;
        std::cout << "ovrldng_4 var =" << var << std::endl;
    }

    ovrldng_4() : ovrldng_4(101){}
};

To test these you can just create an object of every type (and with every defined ctor) and check the output:

int main(int argc, char* argv[])
{
    ovrldng_1 a;
    ovrldng_1 b(212);
    ovrldng_2 c;
    ovrldng_2 d(212);
    ovrldng_3 e;
    ovrldng_3 f(212);
    ovrldng_4 g;
    ovrldng_4 h(212);
    return 0;
}

Output:

ovrldng_1 var = 101
ovrldng_1 var = 212
ovrldng_2 var = 101
ovrldng_2 var = 212
ovrldng_3 var = 101
ovrldng_3 var = 212
ovrldng_4 var = 101
ovrldng_4 var = 212

Addendum You should use initializer lists (see https://isocpp.org/wiki/faq/ctors#init-lists ). I only didn't use them to make the different classes easier to compare.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM