CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 15 of 29

Threaded View

  1. #1
    John E is offline Elite Member Power Poster
    Join Date
    Apr 2001
    Location
    Manchester, England
    Posts
    4,867

    Are these c'tors legal or dangerous?

    I've just come across this code in a 3rd party library which seems otherwise very professionally written....

    Code:
    // Declaration
    class CMyClass
    {
    public:
    	CMyClass(void);
    	CMyClass(char* name);
    	virtual ~CMyClass(void);
    
    // Data
    private:
    	char* name;
    
    public:
    	char* GetName(void) { return name; }
    };
    
    
    // Constructor #1
    CMyClass::CMyClass(void)
    : name("Default name")
    {
    }
    
    
    // Constructor #2
    CMyClass::CMyClass(char* szName)
    : name(szName)
    {
    }
    The class seems to work but my question is whether or not these c'tors are dangerous. The member function CMyClass::GetName() successfully returns either the supplied name or the default name depending on which c'tor was used - but it worries me that no memory's been allocated anywhere.

    C'tor #2 makes reasonable sense. It's c'tor #1 that worries me. Is the string "Default name" guaranteed to exist for the duration of the object?
    Last edited by John E; October 28th, 2006 at 10:04 AM.
    "A problem well stated is a problem half solved.” - Charles F. Kettering

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured