473,396 Members | 1,998 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Passing speicifc class elements as arguments without explicitly listing them - is it possible?

KK
Hello all,
I have several classes binded by one common interface - say 'sum'
interface which calculates the sum of all the class elements of type
'int'.

class Alphabet
{
int _a;
int _b;
int _c;
char _ch; // some other stray element
friend double sumInterface( int first, ... );
public:
double _sum; // ' double' type introduced to differentiate from above
core elements
Alphabet( void)
{
_a=_b=_c=2;
_sum=sumInterface(_a,_b,_c,-1);
}
};

class Numbers
{
int _one;
int _two;
friend double sumInterface( int first, ... );
public:
double _sum;
Numbers(void)
{
_one=1;
_two=2;
_sum=sumInterface(_one,_two,-1);
}
};

double sumInterface( int first, ... )
{
int i = first;
double sum=0;
va_list marker;
va_start( marker, first ); /* Initialize variable arguments. */
while( i != -1 ) //-1 acts as delimiter
{
sum += i;
i = va_arg( marker, int);
}
va_end( marker ); /* Reset variable arguments. */
return sum ;
}

The problem with this approach is that I need to provide all the class
elements to this function each type I invoke it.

_sum=sumInterface(_a,_b,_c,-1);
_sum=sumInterface(_one,_two,-1);

Is there anyway I make the function to understand to consider all the
class elements of type int so that I dont have to explicitly type all
the elements?

If yes, can I general this concept to other user defined data types?

Also, little off topic but is there a way I can avoid using a delimiter
in the variable length arguments ?

Thank you.
-KK

Dec 2 '05 #1
3 1572
I don't see any way to do this in C++. OTOH, you might be able to use
SWIG to sort of reflect upon your class, generating XML, then generate
the code you need by parsing the XML.

Noel

Dec 2 '05 #2
KK wrote:
Hello all,
I have several classes binded by one common interface - say 'sum'
interface which calculates the sum of all the class elements of type
'int'.

class Alphabet
{
int _a;
int _b;
int _c;
char _ch; // some other stray element
Why not just add a method:
double evalsum()
{
return sumInterface(_a,_b,_c,-1);
}

To all the classes you care about ?
....
The problem with this approach is that I need to provide all the class
elements to this function each type I invoke it.

_sum=sumInterface(_a,_b,_c,-1);
_sum=sumInterface(_one,_two,-1);
This would become:

_sum=evalsum();

Is there anyway I make the function to understand to consider all the
class elements of type int so that I dont have to explicitly type all
the elements?
Not possible with standard C++.

If yes, can I general this concept to other user defined data types?

Also, little off topic but is there a way I can avoid using a delimiter
in the variable length arguments ?


Use overloaded functions.

double sum( int v1 )
{
return double(v1);
}

double sum( int v1, int v2 )
{
return double(v1) + v2;
}

double sum( int v1, int v2, int v3 )
{
return double(v1) + v2 + v3;
}

....

To as many as you care about

Dec 2 '05 #3
KK wrote:
Hello all,
I have several classes binded by one common interface - say 'sum'
interface which calculates the sum of all the class elements of type
'int'.

class Alphabet
{
int _a;
int _b;
int _c;
char _ch; // some other stray element
friend double sumInterface( int first, ... );
public:
double _sum; // ' double' type introduced to differentiate from above
core elements
Alphabet( void)
{
_a=_b=_c=2;
_sum=sumInterface(_a,_b,_c,-1);
}
};

class Numbers
{
int _one;
int _two;
friend double sumInterface( int first, ... );
public:
double _sum;
Numbers(void)
{
_one=1;
_two=2;
_sum=sumInterface(_one,_two,-1);
}
};

double sumInterface( int first, ... )
{
int i = first;
double sum=0;
va_list marker;
va_start( marker, first ); /* Initialize variable arguments. */
while( i != -1 ) //-1 acts as delimiter
{
sum += i;
i = va_arg( marker, int);
}
va_end( marker ); /* Reset variable arguments. */
return sum ;
}

The problem with this approach is that I need to provide all the class
elements to this function each type I invoke it.

_sum=sumInterface(_a,_b,_c,-1);
_sum=sumInterface(_one,_two,-1);

Is there anyway I make the function to understand to consider all the
class elements of type int so that I dont have to explicitly type all
the elements?
No.
If yes, can I general this concept to other user defined data types?
Depending on your design, you may be better with containers. These
classes seem like container-like classes, so you could either

1) typedef them to standard containers

typedef std::vector<int> Alphabet;
typedef std::vector<int> Numbers;

void f(Alphabet &a)
{
int sum = std::accumulate(a.begin(), a.end(), 0);
}

2) use standard containers internally and provide a (standard if
possible) container interface

class Alphabet
{
public:
typedef Cont::iterator iterator;

iterator begin()
{
return cont_.begin();
}

iterator end()
{
return cont_.end();
}

private:
typedef std::vector<int> Cont;
Cont cont_;
};

void f(Alphabet &a)
{
// note: this does not change
int sum = std::accumulate(a.begin(), a.end(), 0);
}

3) Return a container from a member function containing all the values.
That would fit if the class is not really a container. This would wrap
all the values in a container to calculate the sum (or something else)
easily.

class Alphabet
{
public:
typedef std::vector<int> Container;

Container cont() const
{
Container c;
c.push_back(a_);
c.push_back(b_);

return c;
}

private:
int a_, b_;
};

4) Use the sum computed by a member function, which removes the need
for "do-it-all" free function

class Alphabet
{
public:
int sum() const
{
return a_ + b_;
}

private:
int a_, b_;
};

void f(Alphabet &a)
{
int sum = a.sum();
}
You may use inheritance or templates if you need to work with multiple
classes. For example, f() could be rewritten as

template <class C>
int sum(C &c)
{
int sum = std::accumulate(c.begin(), c.end(), 0);

// or

int sum = c.sum();
}

As you see, you are not short of options.
Also, little off topic but is there a way I can avoid using a delimiter
in the variable length arguments ?


Yes, don't use variable length arguments.
Jonathan

Dec 2 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

50
by: Dan Perl | last post by:
There is something with initializing mutable class attributes that I am struggling with. I'll use an example to explain: class Father: attr1=None # this is OK attr2= # this is wrong...
3
by: domeceo | last post by:
can anyone tell me why I cannot pass values in a setTimeout function whenever I use this function it says "menu is undefined" after th alert. function imgOff(menu, num) { if (document.images) {...
15
by: Mon | last post by:
I am in the process of reorganizing my code and came across and I came across a problem, as described in the subject line of this posting. I have many classes that have instances of other classes...
1
by: Augasm | last post by:
I'm trying to clean up a bunch of my code by building C++ classes for commonly used code elements, and have run into a problem. Allow me to preface this with a disclaimer that I am not formally...
6
by: Adam Hartshorne | last post by:
Hi All, I have the following setup. Two 'std::vector's which i iterate through in a for (iterate through vector1 of types X) { for (iterate through vector2 of types Y) { f(x) }
7
by: Harolds | last post by:
The code below worked in VS 2003 & dotnet framework 1.1 but now in VS 2005 the pmID is evaluated to "" instead of what the value is set to: .... xmlItems.Document = pmXML // Add the pmID...
2
by: Martin Gernhard | last post by:
Hi, I'm trying to use expression templates to calculate values at compile time. Doing it with just one parameter is no problem: ///Begin Listing 1 #include <iostream> using namespace std; ...
17
by: =?Utf-8?B?U2hhcm9u?= | last post by:
Hi Gurus, I need to transfer a jagged array of byte by reference to unmanaged function, The unmanaged code should changed the values of the array, and when the unmanaged function returns I need...
1
by: Ben Warren | last post by:
Hello, Let's say I have a function with a variable number of arguments(please ignore syntax errors): def myfunc(a,b,c,d,...): and I have a tuple whose contents I want to pass to the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.