Object initialization

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

    #1

    Object initialization

    This question deals with unmanaged C++ (Visual Studio 2005).

    Suppose I have a class named MyClass with a constructor that takes an
    argument. How do I create an instance of type MyClass as a member variable
    (see below)?

    Class Test
    {
    public:
    Test(void); // Constructor
    ~Test(void); // Destructor

    private:
    MyClass m_myClass("My argument"); // error C2059; syntax error :
    'constant'
    };

    TIA - Bob


  • David Lowndes

    #2
    Re: Object initialization

    >Suppose I have a class named MyClass with a constructor that takes an
    >argument. How do I create an instance of type MyClass as a member variable
    >(see below)?
    >
    Class Test
    {
    public:
    Test(void); // Constructor
    ~Test(void); // Destructor
    >
    private:
    MyClass m_myClass("My argument"); // error C2059; syntax error :
    >'constant'
    };
    Bob,

    I think you'd do it like this:


    Test() : m_myClass( "Hello" )
    {
    }

    private:
    MyClass m_myClass;

    Dave

    Comment

    • Bob Altman

      #3
      Re: Object initialization

      Thanks!

      - Bob

      "David Lowndes" <DavidL@example .invalidwrote in message
      news:bv6083l97g e66qmofsbtn210h kqk40irpb@4ax.c om...
      Suppose I have a class named MyClass with a constructor that takes an
      >>argument. How do I create an instance of type MyClass as a member
      >>variable
      >>(see below)?
      >>
      > Class Test
      > {
      > public:
      > Test(void); // Constructor
      > ~Test(void); // Destructor
      >>
      > private:
      > MyClass m_myClass("My argument"); // error C2059; syntax error :
      >>'constant'
      > };
      >
      Bob,
      >
      I think you'd do it like this:
      >
      >
      Test() : m_myClass( "Hello" )
      {
      }
      >
      private:
      MyClass m_myClass;
      >
      Dave

      Comment

      Working...