Allocating memory. constructor being called

This is a discussion on Allocating memory. constructor being called within the C++ Programming forums, part of the General Programming Boards category; Hello, I am trying to gain access to a int that I have created with new from the constructor of ...

  1. #1
    UK2
    Join Date
    Sep 2003
    Posts
    112

    Allocating memory. constructor being called

    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;
    }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    What do you expect to get, and what do you get? You've got an int *. New does value initialization, doesn't it? Shouldn't you get 0? (And if it's not value initialized, who knows what you're going to get.)
    Last edited by tabstop; 02-02-2009 at 09:50 PM.

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    6,266
    I'm not sure I understand the problem.

    Your base class default constructor (the one with no arguments) allocates a new int.

    The derived class constructor is required to invoke a destructor of the base class (as the base class "part" of an object has to be constructed in order for the derived class to work). If the derived class constructor invokes the default constructor, a new int will be allocated every time a derived class object is instantiated - each distinct instance of the derived class includes data of a distinct instance of the base class.

    Which would suggest you're seeing what you should.

    If you're not seeing what you expect, your expectations are wrong.

    In particular, in your main() function, the object named isdn will not magically access the _lib_handle member in the object named boardservice. The instance of TBBoardService named boardservice is a distinct object from the TBBoardService within isdn.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help -- allocating memory in a multidimensional array
    By jonathan.plumb in forum C Programming
    Replies: 10
    Last Post: 05-20-2009, 11:04 PM
  2. Suggestions on this C style code
    By Joelito in forum C Programming
    Replies: 11
    Last Post: 06-07-2007, 03:22 AM
  3. Dynamically allocating memory...
    By Junior89 in forum C++ Programming
    Replies: 28
    Last Post: 05-08-2007, 10:17 PM
  4. allocating memory in constructor
    By Micko in forum C++ Programming
    Replies: 3
    Last Post: 08-25-2004, 07:45 AM
  5. Replies: 5
    Last Post: 11-24-2002, 10:33 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21