3 Replies - 1719 Views - Last Post: 06 April 2009 - 09:42 AM Rate Topic: -----

#1 Zel2008  Icon User is offline

  • D.I.C Addict

Reputation: 14
  • View blog
  • Posts: 790
  • Joined: 06-January 09

Default parameters

Posted 06 April 2009 - 08:26 AM

Hi everybody,

I have a C++ class that has a data member in it which is an object, and I'd like to use a value of that object as a default parameter to an error method. I know this sounds very convoluted, so I'll just show you the header code, hopefully that will speak for itself:

// Method declaration
int error( int code = object->code() );

// Object declaration
Object object;



As you can see, I'm trying to get the error status of this object, and the default way to do this should be to use an integer that comes from a cal to this object.

So, as my code stands now, I get this error at link time:
invalid use of non-static data member 'object'

If I make 'object' static, I get this error:
undefined reference to 'object'

I have a feeling there's something with syntax I'm missing; can anyone please help me figure out what it is?

Thanks,
Zel2008

Is This A Good Question/Topic? 0
  • +

Replies To: Default parameters

#2 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3102
  • View blog
  • Posts: 19,142
  • Joined: 14-September 07

Re: Default parameters

Posted 06 April 2009 - 08:38 AM

You're probably calling object before object is defined. Move object up above the prototype.

Although, I've seen this type of code a lot recently....why? If you're going to have the param always be the same, just access the object's data inside the function. Pass the object to it even.



Ilustrating my point with primitives:

#include <iostream>
#include <vector>
using namespace std;

int myVar = 5;
int function (int num = myVar);

int main()
{
	cout << function(myVar) << endl;
	return 0;
}

int function(int num)
{
	return num;
}



The above is terrible coding practice, but legal.


edit: Here is an example using a class object, again horrible coding, why would you want to do this? Might as well make a function with no params or again, a reference to the object:

#include <iostream>
#include <vector>
using namespace std;

class Object {
private:
	int code;
public:
	Object(int n)			{code=n;};
	~Object()				{};
	int getCode()			{return code;};
};

Object myObject (5);

int function (int num = myObject.getCode());

int main()
{
	cout << function() << endl;
	return 0;
}

int function(int num)
{
	return num;
}


This post has been edited by KYA: 06 April 2009 - 08:42 AM

Was This Post Helpful? 0
  • +
  • -

#3 Zel2008  Icon User is offline

  • D.I.C Addict

Reputation: 14
  • View blog
  • Posts: 790
  • Joined: 06-January 09

Re: Default parameters

Posted 06 April 2009 - 08:48 AM

Thanks KYA,
If the coding practice is so horrible, I guess I'll just go for two overloaded methods, then I can access the object inside one and use an int paramter for the other, and have them call each other. I guess I was getting too overzealous with default parameters; thanks for the advice.

Thanks,
Zel2008

View PostKYA, on 6 Apr, 2009 - 07:38 AM, said:

You're probably calling object before object is defined. Move object up above the prototype.

Although, I've seen this type of code a lot recently....why? If you're going to have the param always be the same, just access the object's data inside the function. Pass the object to it even.



Ilustrating my point with primitives:

#include <iostream>
#include <vector>
using namespace std;

int myVar = 5;
int function (int num = myVar);

int main()
{
	cout << function(myVar) << endl;
	return 0;
}

int function(int num)
{
	return num;
}



The above is terrible coding practice, but legal.


edit: Here is an example using a class object, again horrible coding, why would you want to do this? Might as well make a function with no params or again, a reference to the object:

#include <iostream>
#include <vector>
using namespace std;

class Object {
private:
	int code;
public:
	Object(int n)			{code=n;};
	~Object()				{};
	int getCode()			{return code;};
};

Object myObject (5);

int function (int num = myObject.getCode());

int main()
{
	cout << function() << endl;
	return 0;
}

int function(int num)
{
	return num;
}


Was This Post Helpful? 0
  • +
  • -

#4 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3102
  • View blog
  • Posts: 19,142
  • Joined: 14-September 07

Re: Default parameters

Posted 06 April 2009 - 09:42 AM

It's only horrible if you have to make a global object to make it work. If you can do it without it, then by all means use default params.

This covers default params pretty well.


edit: the main problem with using an object's function is that it may or may not be initialized, you'd have to have a global/static object that can be used as the default param.

i.e. what object? is what the program will ask...where do I look for it? How do I access it? so on and so forth...

This post has been edited by KYA: 06 April 2009 - 09:44 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1