LinuxQuestions.org
Go Job Hunting at the LQ Job Marketplace
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 04-08-2005, 02:36 PM   #1
R00ts
Member
 
Registered: Mar 2004
Location: Austin TX, USA
Distribution: Ubuntu 11.10, Fedora 16
Posts: 545

Rep: Reputation: 30
C++, indefinite function arguments and multiple constructor passing @_@


I have a rather esoteric question to ask, so let me first try to put my question in English and then show some sample code.


Question: I have a handful of classes that are singletons (by keeping track of the number of references). I only allow the classes to be constructed via a _Create() function, which invokes the new operator if the object does not exist and returns a pointer to the object. Is it possible/safe for me to pass an indefinite number of arguments (via the ellipsis '...') to the _Create() function that in turn passes those arguments to the classes' constructor? In other words, I want to be able to call multiple constructors via the arguments I pass to the _Create() function.


So it's something like this:

Code:
class SKELETON {
private:
	static SKELETON *_ref;
	static int _ref_count;
	SKELETON(); // No-arg constructor
	SKELETON(int val); // Constructor with argument
	~SKELETON();
	SKELETON(const SKELETON& );
	SKELETON& operator=(const SKELETON& );
protected:
	int value;
;
public:
	static SKELETON* _Create(...);
	static void _Destroy();
	static int _GetRefCount();
	void set_value(int val) { value = val; }
	int get_value() { return value; }
};

SKELETON* SKELETON::_ref = NULL;
int SKELETON::_ref_count = 0;

inline SKELETON* SKELETON::_Create(...) {
	if (_ref == NULL) {
		_ref = new SKELETON(...); // Somehow pass the arguments to the constructor
		_ref_count = 1;
	} 
	else
		_ref_count++;
	return _ref;
}

inline void SKELETON::_Destroy() {
	if (--_ref_count == 0) {
		delete _ref;
		_ref = NULL;
	}
}

inline int SKELETON::_GetRefCount() {
	return _ref_count;
}


int main() {
  SKELETON *noarg = SKELETON::_Create(); // Creates class object with default value
  SKELETON::_Destroy(); // Deletes the class, since this is the last instance

  SKELETON *witharg = SKELETON::_Create(5); // Create the class object with value = 5
  SKELETON::_Destroy(); // Memory leaks are bad!
  return 0;
}

The code won't compile because passing the ... to the constructor doesn't work. I knew it wouldn't, but I'm not sure how to do this operation. So is this possible/feasible? Thanks for any insight.
 
Old 04-08-2005, 03:15 PM   #2
Mara
Moderator
 
Registered: Feb 2002
Location: Grenoble
Distribution: Debian
Posts: 9,536

Rep: Reputation: 148Reputation: 148
Quote:
Is it possible/safe for me to pass an indefinite number of arguments (via the ellipsis '...') to the _Create() function that in turn passes those arguments to the classes' constructor?
Yes, it's possible. Look at va_start, va_end and similar functions (they have man pages). Safe? If you write the constructor in such a way that it won't crash etc receiving such parameters, it's OK. Just be as careful as usual.
 
Old 04-08-2005, 03:33 PM   #3
R00ts
Member
 
Registered: Mar 2004
Location: Austin TX, USA
Distribution: Ubuntu 11.10, Fedora 16
Posts: 545

Original Poster
Rep: Reputation: 30
Awesome, thank you.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Problem with passing arguments in Perl bahadur Programming 1 05-30-2005 01:47 AM
Passing Arguments into the Thread Function George_gk Programming 2 01-31-2005 05:03 AM
A main can be changed by a function local without passing anything to the function? ananthbv Programming 10 05-04-2004 01:31 PM
Handline passing arguments in C AMMullan Programming 9 03-22-2004 01:37 AM


All times are GMT -5. The time now is 01:38 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration