Instantiate class with non default constructor

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Richard

    #1

    Instantiate class with non default constructor

    I have two classes ClassA and ClassB.

    ClassB has a constructor that has ClassA as it's parameter. How do I make
    ClassB as a member object of ClassA so that ClassA (i.e. 'this') is passed
    into ClassB on instatiation?


  • John Harrison

    #2
    Re: Instantiate class with non default constructor

    Richard wrote:[color=blue]
    > I have two classes ClassA and ClassB.
    >
    > ClassB has a constructor that has ClassA as it's parameter. How do I make
    > ClassB as a member object of ClassA so that ClassA (i.e. 'this') is passed
    > into ClassB on instatiation?
    >
    >[/color]

    class ClassA
    {
    public:
    ClassA() : b(this) {}
    private:
    ClassB b;
    };

    Some compilers will give you warnings about this code because you are
    using the this pointer before the ClassA object is constructed. But
    provided you don't try and dereference the pointer in the ClassB
    constructor it is OK.

    john

    Comment

    • twoeyedhuman1111

      #3
      Re: Instantiate class with non default constructor

      Your name better not be Richard Stallman...

      Comment

      • Richard

        #4
        Re: Instantiate class with non default constructor

        "twoeyedhuman11 11" <twoeyedhuman11 11@gmail.com> wrote in message
        news:1132012861 .773774.152380@ f14g2000cwb.goo glegroups.com.. .[color=blue]
        > Your name better not be Richard Stallman...[/color]

        Nope my name is Richard Head.


        Comment

        Working...