0

i have problem withe this code i want to create a pointer to new class

class AnalyzerManager
{

public:

    AnalyzerManager();
    ~AnalyzerManager();
private:
    CAnalyzerIF* m_AnalyzerIF = new CAnalyzerIF();

};

it has a compilation error i dont understand how to fix it. thanks

wkl
  • 77,184
  • 16
  • 165
  • 176
adir
  • 1,257
  • 2
  • 16
  • 22

4 Answers4

4

You have to initialize the pointer in your constructor, not in the class declaration.

AnalyzerManager::AnalyzerManager() : m_AnalyzerIF(new CAnalyzerIF())
{
}

BTW, you might want to look into smart pointers for something like this.

Community
  • 1
  • 1
Fred Larson
  • 60,987
  • 18
  • 112
  • 174
1
// in AnalyzerManager.h
#include <memory>

class AnalyzerManager
{
public:
    AnalyzerManager();
    ~AnalyzerManager();

 // since you are declaring a custom constructor and destructor, you should also define a custom copy-constructor and copy-assignment
 AnalyzerManager(const AnalyzerManager& rhs);
 AnalyzerManager& operator= (const AnalyzerManager& rhs);
private:
    std::shared_ptr<CAnalyzerIF> m_AnalyzerIF;
};

// in AnalyzerManager.cpp
AnalyzerManager::AnalyzerManager() : m_AnalyzerIF(new CAnalyzer)
{

}

AnalyzerManager::~AnalyzerManager()
{
    // nothing to do since shared_ptr will clean up the memory for us 
}

AnalyzerManager::AnalyzerManager(const AnalyzerManager& rhs) : m_AnalyzerIF(rhs.m_AnalyzerIF)
{

}

AnalyzerManager& AnalyzerManager::operator= (const AnalyzerManager& rhs)
{
    m_AnalyzerIF = rhs.m_AnalyzerIF;
 return *this;
}
Zac Howland
  • 15,777
  • 1
  • 26
  • 42
  • Note that this requires C++0x. Otherwise, use boost::shared_ptr or std::tr1::shared_ptr. – Fred Larson Jan 06 '11 at 19:31
  • i didnt understand this line : AnalyzerManager& operator= (const AnalyzerManager& rhs); – adir Jan 06 '11 at 19:31
  • @Fred: Ah, thanks. I forgot to add that caviat :) @adir: That declares a copy-assignment operator. As a general rule, if you declare a custom constructor or destructor, you probably should also declare a custom copy-constructor and copy-assignment operator. See the Rule of Three for more information. – Zac Howland Jan 06 '11 at 19:40
  • Also note that this does shallow copying, which may or may not be desired (very likely not). – Fred Larson Jan 06 '11 at 19:46
  • Without knowing the requirements of the system, it is difficult to state. For simplicity, I left it as a shallow copy. To make it a deep copy, you'd simply need to change the copy constructor and copy-assignment operator to copy the object instead of the pointer: `m_AnalyzerIF = std::shared_ptr(new CAnalyzer(*(rhs.m_AnalyzerIF)))` (which will use the copy constructor for CAnalyzer). – Zac Howland Jan 06 '11 at 20:14
1

You should start by asking yourself : When do I want this 'new' statement to be executed?

Assuming the answer is on construction of an AnalyzerManager object, then the place for the 'new' is in the constructor of the AnalyzerManager.

Code placed in a constructor that initialises member variables is typically done using the member initialisation list, like this :

AnalyzerManager::AnalyzerManager() : m_AnalyzerIF(new CAnalyzerIF())
{
}

Since you are using 'new' you should consider where the 'delete' will go - presumably in the AnalyzerManager destructor :

AnalyzerManager::~AnalyzerManager()
{
    delete m_AnalyzerIF;
}
NexusSquared
  • 157
  • 1
  • 7
0

You cannot do like you have written. I mean right in the declaration.

CAnalyzerIF* m_AnalyzerIF = new CAnalyzerIF();

Instead you have to do that in the constructor AnalyzerManager().

AnalyzerManager::AnalyzerManager()
{
  m_AnalyzerIF = new CAnalyzerIF();
  // ... and some more initialization ...
}
yasouser
  • 5,113
  • 2
  • 27
  • 41