473,226 Members | 1,405 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,226 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 1562
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: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...

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.