0

hi all. I'm not sure if its possible but can you call a different constructor inside a constructor of a object? ie can i have different constructors call a single constructor to set up the object

class foo
{
public:
       foo(int)
       foo(double)
       //...
};

foo::foo(int number)
{
       this = foo((double)number);  // ?
}

any help would be appreciated. thanks

2
Contributors
1
Reply
2
Views
8 Years
Discussion Span
Last Post by firstPerson
0

yes sure you can. For example you might have a reset method that
resets everything to a default constructor :

class A
{
    int c;
    public : 
   A() { c = 1000; }
    A(int x ) { c = x; }
     void reset() { *this = A(0); }     
};
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.