LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-21-2009, 06:56 PM   #1
pilotmm
LQ Newbie
 
Registered: Aug 2007
Distribution: openSuse
Posts: 26

Rep: Reputation: 2
g++ calling the deconstructor, and not the constructor in pass by value functions


Hello All

I have a memory container that I use to help me manage memory. It helps me share memory between different module of my program and deletes the memory when the memory is not needed.

I also have an template abstract base class that I use to generalize data access of various classes. Originally, it was for primary types like long, short and such. Hence, the set function accepted arguments by value.

Recently I wrote a class that contains memory containers. I want to use the same generalized data access for these containers. Hence I am passing this memory container class by value. The real size of this class is just a little larger than a pointer, so I figured it would be a minor performance hit.

Problem is when I pass a class by value, a new instance of that class is created. Except, no constructor function is called. Yet, when the function returns, the deconstructor is called and the memory is deleted. Since no function is called on creation, I cannot check/set any static flags to determine whether the memory container is a copy (hence avoid deleting the object)

I know an easy solution would be to simply pass the class by reference. However, I would have to edit the source code for all classes that already inherit the generalized data access interface. Also, classes that use this interface would have to pass memory locations. Something like `set(2.1)' would not compile.

I have considered making a separate set_by_reference class. Yet, with that implementation my data access is no longer generalized and I will have to write two versions of all the functions that rely on that generalized interface. Again the solution is a lot of tedious code writing.

I want to avoid breaking my code so I am trying to figure out some more elegant solutions to my problem. I would be happy to hear your thoughts.

Can some wise code guru tell me whether this is compliant with the C++ standard, or is this a bug with g++? Personally, I think it makes sense that there should be a constructor call, for every deconstructor call (and vice versa). Also, I assumed a pass by value call would blindly copy the data on and off the stack. Can someone give some thoughts on how I can control the calling of either the constructor or the deconstructor in a function call? Thanks a lot for the help.

Take Care
PilotMM

Let me give you some sudo code to help describe my problem:

Code:
class memoryContainer
{
   private:
       pointer_to_memory memory;
       info_about_memory info;
   public:
       memoryContainer()
       {
            // Set info for empty memory
                ...
       }
       ~memoryContainer()
       {
           // If memory is set and not needed then delete it
               ...
       }

       // Other functions for setting and getting the memory and info
             ...
};

template<typename T>
class abstract_base_class
{
  public:
    // constructor and deconstructor
         ...
    // Interface for data access
    virtual void set(memoryContainer mem)=0;
         ...
};

class newDataContainingObject:public abstract_base_class<memoryContainer>
{
  public:
    void set(memoryContainer mem)
    {
         // set some other memory container to hold the same memory
    }
};

int main()
{
     memoryContainer mem;
     newDataContainingObject dataObj;
     // Build up one memory container with memory
           ...
     // Set the other memory container with the set function
     dataObj.set(mem); 
     // When this is called a new memoryContainer is created 
     // without the constructor being called
     // The contents of the argument are copied into this new container
     // When the function returns, the deconstructor of the memory 
     // container is called. That is when the internal memory is deleted.
}
 
Old 07-21-2009, 07:00 PM   #2
pilotmm
LQ Newbie
 
Registered: Aug 2007
Distribution: openSuse
Posts: 26

Original Poster
Rep: Reputation: 2
I hope people don't mind, I solved this problem, but am just posting so that others in similar situations can learn from this post.

Reading the post: http://www.linuxquestions.org/questi...return-105282/

I was made aware of the "copy constructor"

Playing around with code, I learned how to ensure that a constructor is called in pass by value functions. Below is a sample of code that will demonstrate this point.

Code:
#include <iostream>

using namespace std;

class defaultCon
{
	public:
		defaultCon()
		{
			cout << "Default Constructor for defaultCon" << endl;
			return;
		}
		~defaultCon()
		{
			cout << "Default deconstructor for defaultCon" << endl;
			return;
		}
};

class copyCon
{
	public:
		copyCon()
		{
			cout << "Default Constructor for copyCon" << endl;
			return;
		}
		copyCon(copyCon &ob1)
		{
			cout << "### Copy Constructor for copyCon ###" << endl;
			return;
		}
		~copyCon()
		{
			cout << "Default deconstructor for copyCon" << endl;
			return;
		}
};

void callDefaultByValue(defaultCon data)
{
	cout << "Called the copy by value function for defaultCon" << endl;
	return;
}

void callCopyByValue(copyCon data)
{
	cout << "Called the copy by value function for copyCon" << endl;
	return;
}

int main()
{
	defaultCon dat1;
	copyCon dat2;

	callDefaultByValue(dat1);
	callCopyByValue(dat2);

	return 0;
}
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
not calling copy constructor on function return jhorvath Programming 7 09-22-2009 12:43 PM
Gtk+-2.0 Calling constructor [language C] Four Programming 3 05-30-2008 02:21 PM
QT calling functions ... How? Four Programming 3 02-11-2007 10:32 AM
calling a superclass constructor kpachopoulos Programming 1 09-04-2006 12:15 PM
Calling functions in C++ oulevon Programming 3 01-28-2005 03:14 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:53 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration