473,473 Members | 1,807 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Coding Problem, Help Needed

Hello to ALL,

I'll explain my problem by pseudo code

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

class B
{
GetVariable(int i)
{
A a = new A();
//Now I want to return the ith varaible
//of class A
}
}

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 the GetVariable(int i), in VC++
The reason I have class B is because there are lot's of classes
like class A, and i don't want to edit them all.

Any Suggestions...

Thanks In advance...
Cheers...

Bye

Nov 17 '05 #1
3 990
"batista" <sa*********@yahoo.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
Hello to ALL,

I'll explain my problem by pseudo code

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

class B
{
GetVariable(int i)
{
A a = new A();
//Now I want to return the ith varaible
//of class A
}
}

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 the GetVariable(int i), in VC++
The reason I have class B is because there are lot's of classes
like class A, and i don't want to edit them all.

Any Suggestions...


x, y, and z are private to A. So you need public accessor functions in A or
you need to make B a friend of A.

Regards,
Will
Nov 17 '05 #2
C++ is a strongly typed language. You can't write a function that
returns different types based on its input argument. The only way is to
use a variant type. There are many existing implementations, or you can
write your own. The key is to write a class that can change its type
dynamically. The easiest way is using a union:

struct MyVariant
{
enum VarType { IntType, CharType, StringType };
VarType type;
union
{
int i;
char c;
char* s;
};
};

Of course this is unfinished, but it gets you started. Take a look at
the VARIANT data type in COM, although it's pure C, it also has a C++
wrapper in ATL.

Note that variants introduce complexity and runtime overhead to your
code. Obviously a GetVariable function like yours will run way slower
than accessing x, y, and z directly.

Tom

batista wrote:
Hello to ALL,

I'll explain my problem by pseudo code

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

class B
{
GetVariable(int i)
{
A a = new A();
//Now I want to return the ith varaible
//of class A
}
}

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 the GetVariable(int i), in VC++
The reason I have class B is because there are lot's of classes
like class A, and i don't want to edit them all.

Any Suggestions...

Thanks In advance...
Cheers...

Bye

Nov 17 '05 #3
Thanks For The Replies,

I got the point.

Nov 17 '05 #4

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

Similar topics

25
by: evanescent.lurker | last post by:
Hi there, I was wondering is there a point in optimization in code style... I have used to put parenthesis around some php keywords, but they are not needed. For example, I always use: echo(...
136
by: Merrill & Michele | last post by:
A derangement is a mapping of a set onto itself leaving no element fixed. I realized that that was what I was programming when I was asked to randomly determine who buys presents for whom this...
144
by: Natt Serrasalmus | last post by:
After years of operating without any coding standards whatsoever, the company that I recently started working for has decided that it might be a good idea to have some. I'm involved in this...
4
by: Josh Golden | last post by:
i lead a small development team (based on some of my posts that might cause some people to choke themselves, but have no fear, i am NOT the lead developer, the people on my team are great - i'm...
10
by: Ren | last post by:
Hi All, I'm still rather new at vb.net and would like to know the proper way to access private varibables in a class. Do I access the variable directly or do I use the public property? ...
42
by: kiplring | last post by:
1. int intArr = new int; 2. int intArr = new int; rgnNumberArr.Initialize(); 3. int intArr = new int; for( int i=0; i<intArr .lenght; i++)
86
by: PTY | last post by:
Which is better? lst = while lst: lst.pop() OR while len(lst) 0:
3
by: bh | last post by:
If I want to loop through the values in a listbox, and get either all items in the box, or only selected items, based on a boolean variable passed into a subroutine, which method would be more...
19
by: davidsamith | last post by:
'Good code' is code that works, is bug free, and is readable and maintainable. Standards need to be followed for coding. Read more... http://brsx.co.uk/SWtesting/FAQs/FAQs012.asp
23
by: LayneMitch via WebmasterKB.com | last post by:
Hello. It's me again with another question that should generate a lot of responses. As mentioned before, I'm working on my first site (hopefully I'll be done soon and can move on to other...
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
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
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.