How to call default constructor from other constructor of the same class?

Discussion in 'C++' started by romayankin@gmail.com, Mar 6, 2006.

  1. Guest

    Here is the problem. There are two constructors of the same class
    ~~~~~~~~~~~~
    CClass()
    : param1 (0.5),
    param2 (100000),
    param3 (NULL),
    szPath (NULL)
    {

    }
    CClass(char *path)
    : szPath(path)
    {

    }
    //Assume we have the following object
    CClass cc_path(szSomePath);
    ~~~~~~~~~~~~

    Is it possible to call CClass() initialization list, before call to
    CClass(char *path)? Well an obvious workaround is to create some
    private member function that will contain all instantiations but is it
    possible to do something leaving everything intact and just adding some
    code?
    , Mar 6, 2006
    #1
    1. Advertising

  2. Greg Guest

    wrote:
    > Here is the problem. There are two constructors of the same class
    > ~~~~~~~~~~~~
    > CClass()
    > : param1 (0.5),
    > param2 (100000),
    > param3 (NULL),
    > szPath (NULL)
    > {
    >
    > }
    > CClass(char *path)
    > : szPath(path)
    > {
    >
    > }
    > //Assume we have the following object
    > CClass cc_path(szSomePath);
    > ~~~~~~~~~~~~
    >
    > Is it possible to call CClass() initialization list, before call to
    > CClass(char *path)? Well an obvious workaround is to create some
    > private member function that will contain all instantiations but is it
    > possible to do something leaving everything intact and just adding some
    > code?


    No, at least not in the current C++ standard. Perhaps the next major
    revision of C++ will support "delegating" constructors (much like Java
    does).

    But about the best that you can do currently, is to consolidate the
    initialization logic in a common method, usually called Init() or
    something similar. Not an ideal solution, but not a horrible one
    either, in my opinion.

    Greg
    Greg, Mar 6, 2006
    #2
    1. Advertising

  3. Guest

    Semantics of intialization list does not allow calling a constructor
    from other constructor of the same class.

    And the workaround you are talking about is not pure intializtion it is
    an assignment.

    regards
    vishal sharma
    , Mar 6, 2006
    #3
  4. Re: How to call default constructor from other constructor of thesame class?

    * :
    > Is it possible to call CClass() initialization list, before call to
    > CClass(char *path)?


    No.

    This is a FAQ,
    <url: http://www.parashift.com/c++-faq-lite/ctors.html#faq-10.3>.

    --
    A: Because it messes up the order in which people normally read text.
    Q: Why is it such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet and in e-mail?
    Alf P. Steinbach, Mar 6, 2006
    #4
  5. wrote:
    > Here is the problem. There are two constructors of the same class
    > ~~~~~~~~~~~~
    > CClass() : param1 (0.5), param2 (100000), param3 (NULL), szPath (NULL)
    > {}


    > CClass(char *path) : szPath(path)
    > {}


    > //Assume we have the following object
    > CClass cc_path(szSomePath);
    > ~~~~~~~~~~~~
    >
    > Is it possible to call CClass() initialization list, before call to
    > CClass(char *path)?


    I was wondering why would you want to do that at the first place (May
    be you have no control over the source code, but then what was the
    original reason to skip initialization of other member variables in the
    second constructor?

    >Well an obvious workaround is to create some
    > private member function that will contain all instantiations


    no, thats not a workaround. You are talking about assignment and
    actually expecting an initialization.

    > but is it possible to do something leaving everything intact and just adding some
    > code?


    CClass(char *path) : szPath(path) ,param1 (0.5), param2 (100000),
    param3 (NULL)
    {}

    Oh, I believe thats not what you expected, by I "just added some code,
    leaving everything intact" you see ...
    Neelesh Bodas, Mar 6, 2006
    #5
  6. Guest

    wrote:
    > but is it
    > possible to do something leaving everything intact and just adding some
    > code?


    Sure there is. In CClass(char * path) after the : and before
    szPath(path) add the following code:

    param1 (0.5),
    param2 (100000),
    param3 (NULL),
    , Mar 6, 2006
    #6
  7. Guest

    >> what was the original reason to skip initialization of other member variables in the
    second constructor?
    Neelesh, I didn't want to have the same code in two places.


    I was able to come up with following ugly option:
    ~~~~~~~~~~~~~~~
    CClass()
    : param1 (0.5),
    param2 (100000),
    param3 (NULL),
    szPath (NULL)
    {

    }
    CClass(char *path)
    {
    *this = CClass();
    //copy path to szPath here
    }
    ~~~~~~~~~~~~
    but just because, there's nothing better then a simple and straight
    option i'll copy the initialization list to the second constructor as
    well.
    I guess that's it. Thank you All!

    -R.
    , Mar 6, 2006
    #7
  8. Fei Liu Guest

    wrote:
    > >> what was the original reason to skip initialization of other member variables in the

    > second constructor?
    > Neelesh, I didn't want to have the same code in two places.
    >
    >
    > I was able to come up with following ugly option:
    > ~~~~~~~~~~~~~~~
    > CClass()
    > : param1 (0.5),
    > param2 (100000),
    > param3 (NULL),
    > szPath (NULL)
    > {
    >
    > }
    > CClass(char *path)
    > {
    > *this = CClass();
    > //copy path to szPath here
    > }
    > ~~~~~~~~~~~~
    > but just because, there's nothing better then a simple and straight
    > option i'll copy the initialization list to the second constructor as
    > well.
    > I guess that's it. Thank you All!
    >
    > -R.


    What about default parameter list?
    CClass(float param1 = 0.5, int param2 = 100000, char * param3 = 0, char
    * szPath = 0){
    .....
    }
    Fei Liu, Mar 6, 2006
    #8
  9. Marcus Kwok Guest

    Fei Liu <> wrote:
    > What about default parameter list?
    > CClass(float param1 = 0.5, int param2 = 100000, char * param3 = 0, char
    > * szPath = 0){
    > ....
    > }


    I suggested this to someone else in a different situation, and they
    pointed out that default parameters can affect the interface. For this
    example, this default parameter list will have the effect of making
    available the following constructors, which may not necessarily be
    desirable:

    CClass();
    CClass(float param1);
    CClass(float param1, int param2);
    CClass(float param1, int param2, char* param3);
    CClass(float param1, int param2, char* param3, char* szPath);

    For example, the class may be designed such that if any of {param1,
    param2, param3} are provided, then all of them must be provided, in
    which case the extra constructors should not be there.

    --
    Marcus Kwok
    Marcus Kwok, Mar 6, 2006
    #9
  10. Re: How to call default constructor from other constructor of thesame class?

    * Marcus Kwok:
    > Fei Liu <> wrote:
    >> What about default parameter list?
    >> CClass(float param1 = 0.5, int param2 = 100000, char * param3 = 0, char
    >> * szPath = 0){
    >> ....
    >> }

    >
    > I suggested this to someone else in a different situation, and they
    > pointed out that default parameters can affect the interface. For this
    > example, this default parameter list will have the effect of making
    > available the following constructors, which may not necessarily be
    > desirable:
    >
    > CClass();
    > CClass(float param1);
    > CClass(float param1, int param2);
    > CClass(float param1, int param2, char* param3);
    > CClass(float param1, int param2, char* param3, char* szPath);
    >
    > For example, the class may be designed such that if any of {param1,
    > param2, param3} are provided, then all of them must be provided, in
    > which case the extra constructors should not be there.


    Well, you can always use a private base class, or a private class for a
    single data member providing the relevant member items.

    E.g., for the OP's example (I abhor this naming convention, but use the
    exact same example),

    class CClass
    {
    private:
    struct DataMembers
    {
    double param1;
    long param2;
    void const* param3;
    char const* szPath;

    DataMembers( char const* path = NULL )
    : param1( 0.5 )
    , param2( 100000 )
    , param3( NULL )
    , szPath( path )
    {}
    } m;

    public:
    CClass() {}
    CClass(char *path): m( path ) {}
    };

    --
    A: Because it messes up the order in which people normally read text.
    Q: Why is it such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet and in e-mail?
    Alf P. Steinbach, Mar 6, 2006
    #10
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. James.D
    Replies:
    6
    Views:
    398
    James.D
    Mar 5, 2004
  2. Henrik Goldman
    Replies:
    4
    Views:
    428
    Henrik Goldman
    Sep 9, 2006
  3. Rahul
    Replies:
    16
    Views:
    1,179
    terminator
    Nov 7, 2007
  4. Generic Usenet Account
    Replies:
    10
    Views:
    2,205
  5. Karsten Wutzke

    Constructor call in the same class?

    Karsten Wutzke, Jul 25, 2010, in forum: Python
    Replies:
    2
    Views:
    259
    Karsten Wutzke
    Jul 25, 2010
Loading...

Share This Page