Groups | Blog | Home
all groups > visual c > june 2007 >

visual c : Object initialization



Bob Altman
6/25/2007 12:29:54 PM
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

Bob Altman
6/25/2007 1:19:31 PM
Thanks!

- Bob

[quoted text, click to view]

David Lowndes
6/25/2007 8:56:47 PM
[quoted text, click to view]

Bob,

I think you'd do it like this:


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

private:
MyClass m_myClass;

AddThis Social Bookmark Button