Hello,
I am trying to gain access to a int that I have created with new from the constructor of the base class.
I would like to access this from my derived class. However, when the constructor of the derived class gets
called it will call the base class constructor a second time and allocate another memory location.
When I try and get the pointer to the int I get a different value to the one that is assigned.
Any thing I am doing wrong?
Many thanks,
Code:
#ifndef TBBoardService_INCLUDED
#define TBBoardService_INCLUDED
class TBBoardService
{
public:
TBBoardService();
~TBBoardService();
void handle(int *handle);
void open_library();
int* hLibrary(){ return _lib_handle;}
private:
int *_lib_handle;
};
#endif // TBBoardService_INCLUDED
Code:
#include "TBBoardService.hpp"
void TBBoardService::handle(int *handle)
{
*handle = 123456;
}
void TBBoardService::open_library()
{
handle(_lib_handle);
}
TBBoardService::TBBoardService() : _lib_handle(0)
{
_lib_handle = new int;
}
TBBoardService::~TBBoardService()
{
delete _lib_handle;
}
Code:
#include <iostream>
#include "TBIsdnBoardService.hpp"
void TBIsdnBoardService::svc()
{
std::cout << "Start SVC" << std::endl;
int hLib = *(hLibrary()); // cannot get the correct value here
std::cout << "hLib: " << hLib << std::endl;
}
Code:
#include <iostream>
#include "TBBoardService.hpp"
#include "TBIsdnBoardService.hpp"
int main(int argc, char** argv)
{
TBBoardService *boardservice = new TBBoardService();
boardservice->open_library();
std::cout << "_lib_handle: " << *(boardservice->hLibrary()) << std::endl;
TBIsdnBoardService *isdn = new TBIsdnBoardService();
isdn->svc();
getchar();
return 0;
}