Coding Problem, Help Needed

B

batista

Hello to All,

Let me explain my problem with a pseudo code:

class A
{
int x;
char y;
string z;
}

class B
{
GetVariable(int i)
{
A a = new A();
//Now i want to return ith variable of a in
//this function
}
}

class C
{
getvar()
{
B b = new B();
b.GetVariable(0) //it shud return x
b.GetVariable(1) //it shud return y
b.GetVariable(2) //it shud return z
}
}


Now how do i implement GetVariable(int i) function?
The reason i have put class B in between is because
There are many classes like A and i don't want to change
them.

Any Suggestion...

Thanks In Advance...
Cheers...

Bye
 
C

Chris Theis

batista said:
Hello to All,

Let me explain my problem with a pseudo code:

class A
{
int x;
char y;
string z;
}

class B
{
GetVariable(int i)
{
A a = new A();
//Now i want to return ith variable of a in
//this function
}
}

class C
{
getvar()
{
B b = new B();
b.GetVariable(0) //it shud return x
b.GetVariable(1) //it shud return y
b.GetVariable(2) //it shud return z
}
}


Now how do i implement GetVariable(int i) function?
The reason i have put class B in between is because
There are many classes like A and i don't want to change
them.

Any Suggestion...

Thanks In Advance...
Cheers...

Bye

I'm very sorry but your pseudo-code doesn't make very much sense. Could you
please elobarate with real code what you want to achieve? The problem with
your GetVariable(int i) function is that it would have to return different
data types and not only different member variables! However, you need to
specify the return type of a GetVariable function and there is no common
denominator for int/char/string.

Cheers
Chris
 
V

Victor Bazarov

batista said:
Let me explain my problem with a pseudo code:

class A
{
int x;
char y;
string z;
}

class B
{
GetVariable(int i)

What's the return value type?
{
A a = new A();
//Now i want to return ith variable of a in
//this function\

There is no such concept as "ith variable" in C++. Furthermore,
since the variables have different types, you won't be able to
return them from the same function. Plain and simple.
}
}

class C
{
getvar()

Again, what return value type?
{
B b = new B();
b.GetVariable(0) //it shud return x
b.GetVariable(1) //it shud return y
b.GetVariable(2) //it shud return z
}
}


Now how do i implement GetVariable(int i) function?
The reason i have put class B in between is because
There are many classes like A and i don't want to change
them.

Any Suggestion...

Do NOT do it. Post again describing what problem you're trying to solve
and maybe we can help you find a *proper* solution.

If you're trying to implement some kind of "Property" mechanism, look for
it on the web, there are many implementations already available, no sense
to reinvent the wheel, especially when your language skills are not that
advanced.

V
 
M

Mark P

batista said:
Hello to All,

Let me explain my problem with a pseudo code:

class A
{
int x;
char y;
string z;
}

class B
{
GetVariable(int i)
{
A a = new A();
//Now i want to return ith variable of a in
//this function
}
}

class C
{
getvar()
{
B b = new B();
b.GetVariable(0) //it shud return x
b.GetVariable(1) //it shud return y
b.GetVariable(2) //it shud return z
}
}

On top of what has already been mentioned, why do you need to
dynamically allocate "a" and "b"?
 
P

Peter_Julian

| Hello to All,
|
| Let me explain my problem with a pseudo code:
|
| class A
| {
| int x;
| char y;
| string z;
| }

semicolons required!

|
| class B
| {
| GetVariable(int i)
| {
| A a = new A();
| //Now i want to return ith variable of a in
| //this function
| }
| }
|
| class C
| {
| getvar()
| {
| B b = new B();
| b.GetVariable(0) //it shud return x
| b.GetVariable(1) //it shud return y
| b.GetVariable(2) //it shud return z
| }
| }
|
|
| Now how do i implement GetVariable(int i) function?
| The reason i have put class B in between is because
| There are many classes like A and i don't want to change
| them.
|


Doesn't make sense. A's variables are all private in your pseudo code.
That can't be right. I'll assume a struct A.

One way is by deriving from A and overloading getVariable(...). Note:
overloading is not achieved by return type. Overloading involves
parameter types only. So references are used to modify the original
variables.

#include <iostream>
#include <string>

// guessing
struct A
{
int x;
char y;
std::string z;
A(int n, char c, std::string s) : x(n), y(c), z(s) { }
};

class B : public A
{
public:
B(int n, char c, std::string s) : A(n, c, s) { }
~B() { }
void getVariable(int& n) const { n = A::x; }
void getVariable(char& c) const { c = A::y; }
void getVariable(std::string& s) { s = A::z; }
};

int main()
{
B b(99, 'e', "a string");

int x;
b.getVariable(x);
char y;
b.getVariable(y);
std::string z;
b.getVariable(z);

std::cout << "int x = " << x << std::endl;
std::cout << "char y = " << y << std::endl;
std::cout << "int x = " << z << std::endl;

return 0;
}

/*

int x = 99
char y = e
int x = a string

*/

There are many other ways this can be achieved. Since the pseudocode
provided is not accurate, there is no point to continue.
 
B

batista

Thanks For All the Relies,

Victor Bazarov, I don't know how u judged my lanugage skills
But, if u or anyone judged it because of that pseude code
then understand that I only want to ask about that
GetVariable() thing, not that wat shud be private/public or dynamically
allocation etc.

Peter Julian, don't get confused by public/private thing
I know that, Plus

Mark P, there is no reason of dynamically allocating a and b

I know all that

The only Question i want to know is that
is there anything in C++ like ith variable,

That i got the point...

So hope that clears a bit.

Anyway Thanks For the Replies Once Again...

Cheers...

Bye.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top