Passing Objects by Reference.

Started by
22 comments, last by Tradone 18 years ago
What I want done: pass around 1 object. What is being done: 2 objects are created. Which object? : an object from class Config The below are the source files

//main.cpp
#include "Singleton.h"
#include "Print.h"
#include "Cookie.h"
#include "Parameter.h"
#include "Path.h"
#include "Config.h"
#include "Skin.h"

typedef Singleton<Print> print;

int main(){

	Cookie mainCookie;
	Parameter mainParameter;
	Path mainPath;
	Config mainConfig;

	mainParameter.SetParameter();
	mainPath.SetPath();

	print::Instance().SetCookie( mainCookie );
	print::Instance().SetParameter( mainParameter );
	print::Instance().SetPath( mainPath );
	print::Instance().SetConfig( mainConfig );

	if ( mainParameter.TestKey("db") && mainParameter.GetParameterValue("db") != "" ){
		if (  mainParameter.TestKeyType("action") == "input" ){
			//Skin skin(cookie,this,that,last,input.cgi)
			//input action
		}
		else if (  mainParameter.TestKeyType("action") == "delete" ){
			//delete action
		}
		else{
			print::Instance().Header( "success" );
			mainConfig.SetConfig( "./system/db/" + mainParameter.GetParameterValue("db") + "/config.cgi" );
			print::Instance().pConfig();
			std::cout << &mainConfig;
			std::cout << "./system/db/" << mainParameter.GetParameterValue("db") << "/config.cgi";
			Skin skin("system/skins/list.shenu", mainCookie, mainParameter, mainPath, mainConfig );
		}
	}
	else{
		print::Instance().Header( "yesh" );
		print::Instance().Error("no_db", "");
	}

	return 0;
}


//Print.h
#if !defined(PRINT)
#define PRINT
#include <string>
#include <map>
#include <iostream>
#include "Cookie.h"
#include "Parameter.h"
#include "Path.h"
#include "Config.h"
#include "Skin.h"

//header
class Print {
	public:
		Print();

		void Error( std::string, std::string );
		std::string ErrorMessage( std::string, std::string );

		void Header( std::string );
		void Header( std::string, std::string );
		void Body();
		void Body( std::string );
		void Tail();
		void Tail( std::string );

		void pCookie( );
		void pParameter( );
		void pPath( );
		void pConfig( );
		void SetCookie( Cookie& paraCookie );
		void SetParameter( Parameter& paraParameter );
		void SetPath( Path& paraPath );
		void SetConfig( Config& paraConfig );
	//end public:
	private:
		void SoftwareInfo( std::string shenuVersion );
		void CSS( std::map< std::string, std::string > para_fieldData );
		const std::string shenuVersion;

		const static std::string CSSClasses[2][3];
		const static std::string CSSInstances[2][6];

		std::map<std::string, std::string>::const_iterator loop;
		Cookie printCookie;
		Parameter printParameter;
		Path printPath;
		Config printConfig;

	//end private:

};

#endif


//Print.cpp
..
..
void Print::SetConfig( Config& paraConfig ){
	printConfig=paraConfig;
}
..
..







So all in all this is how I set the objects. print::Instance().SetCookie( mainCookie ); print::Instance().SetParameter( mainParameter ); print::Instance().SetPath( mainPath ); print::Instance().SetConfig( mainConfig ); and inside print.h i have Config printConfig; inside print.cpp I have void Print::SetConfig( Config& paraConfig ){ printConfig=paraConfig; } my results: the two objects occupy different memory cells.
Advertisement
Hey bud,

Where you declare:

Config printConfig;

It needs to be:

Config& printConfig;

This means that this variable needs to be initialised on creation which means that you need to assign it on creation in the initialisation list of the class it is a member of, or have it assigned on declaration anywhere else. Remember, you can't have a reference without a value because it needs to reference something from the word go. What you are doing is recieving a reference in the function but then making a copy to the member.

Hope that helps,

Dave
I am going to declare them as pointers.
I've heard pointers are bug prone b/c they just look sophisticated, but I guess that's like my only alternative. maybe...

because I can't initialize those in the beginning.
because the print is a singleton and thus declared in the global, then that would mean I'm going to have to globalize my container types. but I can't do that.

Ok,

You can make those pointer safer by employing the correct use of const. You might also look to auto_ptr which is declared in memory.h as well as the boost pointers.

Hope that helps

Dave
okay...

Cookie* printCookie=NULL;
Parameter* printParameter=NULL;
Path* printPath=NULL;
Config* printConfig=NULL;
or

Cookie* printCookie=0;
Parameter* printParameter=0;
Path* printPath=0;
Config* printConfig=0;

neither of these work I get compile errors as the following:

Print.h:46: error: ISO C++ forbids initialization of member `printCookie'
Print.h:46: error: making `printCookie' static
Print.h:46: error: invalid in-class initialization of static data member of non-integral type `Cookie*'
Print.h:47: error: ISO C++ forbids initialization of member `printParameter'
Print.h:47: error: making `printParameter' static
Print.h:47: error: invalid in-class initialization of static data member of non-integral type `Parameter*'
Print.h:48: error: ISO C++ forbids initialization of member `printPath'
Print.h:48: error: making `printPath' static
Print.h:48: error: invalid in-class initialization of static data member of non-integral type `Path*'
Print.h:49: error: ISO C++ forbids initialization of member `printConfig'
Print.h:49: error: making `printConfig' static
Print.h:49: error: invalid in-class initialization of static data member of non-integral type `Config*'
Quote:Original post by Dave
Ok,

You can make those pointer safer by employing the correct use of const. You might also look to auto_ptr which is declared in memory.h as well as the boost pointers.

Hope that helps

Dave


Thanks.
I know nothing about pointers.
this is my first time using them lool
Are these declared as static variables inside the singleton class? If so, they need to be declared in the CPP file of the singleton, or, by the looks of it, in the main file you posted in the OP.

Dave
This Compiles:
Cookie* printCookie;
Parameter* printParameter;
Path* printPath;
Config* printConfig;


These Don't:
Cookie* printCookie=NULL;
Parameter* printParameter=NULL;
Path* printPath=NULL;
Config* printConfig=NULL;

Cookie* printCookie=0;
Parameter* printParameter=0;
Path* printPath=0;
Config* printConfig=0;

Cookie& printCookie;
Parameter& printParameter;
Path& printPath;
Config& printConfig;


I read somewhere that if I don't null out my pointers I'm setting up time bombs.
okay this gets really funny.
So everything worked out well, even without nulling my pointers.
and when everything worked fine, the results were:

Config:0x2383 (some # i forgot)
Name: [LangList] Value: [euc-kr]
Name: [SnAccessWrite] Value: [1]
Name: [SnAdminHome] Value: [http://www.s]
Name: [SnAdminMail] Value: [yourmail@]
Name: [SnAdminName] Value: [user]
Name: [SnBackAtt] Value: [1]
Name: [SnBackPos] Value: [3]
Name: [SnBackPos2] Value: [2]

Config:0xbfbfe890
Name: [LangList] Value: [euc-kr]
Name: [SnAccessWrite] Value: [1]
Name: [SnAdminHome] Value: [http://www.s]
Name: [SnAdminMail] Value: [yourmail@]
Name: [SnAdminName] Value: [user]
Name: [SnBackAtt] Value: [1]
Name: [SnBackPos] Value: [3]
Name: [SnBackPos2] Value: [2]










So, I performed a little test.

#include "Singleton.h"#include "Print.h"#include "Cookie.h"#include "Parameter.h"#include "Path.h"#include "Config.h"#include "Skin.h"typedef Singleton<Print> print;int main(){	Cookie mainCookie;	Parameter mainParameter;	Path mainPath;	Config mainConfig;	mainParameter.SetParameter();	mainPath.SetPath();	print::Instance().SetCookie( mainCookie );	print::Instance().SetParameter( mainParameter );	print::Instance().SetPath( mainPath );	//print::Instance().SetConfig( mainConfig );	//print::Instance().pPath();	//print::Instance().pParameter();	//print::Instance().pCookie();	//print::Instance().pPath();	if ( mainParameter.TestKey("db") && mainParameter.GetParameterValue("db") != "" ){		if (  mainParameter.TestKeyType("action") == "input" ){			//Skin skin(cookie,this,that,last,input.cgi)			//input action		}		else if (  mainParameter.TestKeyType("action") == "delete" ){			//delete action		}		else{			print::Instance().Header( "success" );			mainConfig.SetConfig( "./system/db/" + mainParameter.GetParameterValue("db") + "/config.cgi" );			print::Instance().pConfig();			std::cout << &mainConfig;			std::cout << "./system/db/" << mainParameter.GetParameterValue("db") << "/config.cgi";			Skin skin("system/skins/list.shenu", mainCookie, mainParameter, mainPath, mainConfig );		}	}	else{		print::Instance().Header( "yesh" );		print::Instance().Error("no_db", "");	}	return 0;}


This is the main thing that has been changed: I commented it out
//print::Instance().SetConfig( mainConfig );

and the results?

Config:0xbfbfe890
Name: [LangList] Value: [euc-kr]
Name: [SnAccessWrite] Value: [1]
Name: [SnAdminHome] Value: [http://www.s]
Name: [SnAdminMail] Value: [yourmail@]
Name: [SnAdminName] Value: [user]
Name: [SnBackAtt] Value: [1]
Name: [SnBackPos] Value: [3]
Name: [SnBackPos2] Value: [2]

the pointer still points to the same thing!!
and the thing is still there!!
what the hell!!
so, I'm guessing that this is the so called memory leakage.
And this is why I should null them.
but I get compile errors when trying to null them.
Quote:Original post by Tradone
This Compiles:
Cookie* printCookie;
Parameter* printParameter;
Path* printPath;
Config* printConfig;


These Don't:
Cookie* printCookie=NULL;
Parameter* printParameter=NULL;
Path* printPath=NULL;
Config* printConfig=NULL;

Cookie* printCookie=0;
Parameter* printParameter=0;
Path* printPath=0;
Config* printConfig=0;

Cookie& printCookie;
Parameter& printParameter;
Path& printPath;
Config& printConfig;


I read somewhere that if I don't null out my pointers I'm setting up time bombs.


You are defining each of them three times. It should look like this:
Cookie* printCookie=NULL;Parameter* printParameter=NULL;Path* printPath=NULL;Config* printConfig=NULL;


Also, what you read is likely just refering to setting them to NULL after (but not before) you delete them, not when you create them.

This topic is closed to new replies.

Advertisement