954,500 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

assigning the address of a variable in c++

assigning the address of a variable in c++
hi all,
im very new to c++ and programming, googled this but couldnt find an answer

if i want a instance of a variable, i can do this

int a = 10;
int & b (a);

so now the address of both a and b are the same, if i change one the other contains the same value as they are both the same address.

now i have a class like so

class testClass {
    public:
        int testInt;
        string testString; 

        testClass() :  testInt(0), testString("") {}  

        testClass(int& i, string& s) {
            testInt = i;
            testString  = s; 
            
        }
};

what i want to do is create 2 instances of this class first and second like so

testClass first;
testClass second;

and here is where i dont know if i can do what i want.
i want to assign first.testString to a value such as "blah"
now i want the address of second.testString to be the same as first.tesString so that they are the same Adress and chaning one changes both. but i dont want the whole class to have the same address... i.e.

i can do
testClass first;
testClass &second(first)

but i dont want that, i want only one of the member variables to have the same addresses

is this possible or is there a better way to do this??

thanks
mono
__________________

monogana
Newbie Poster
12 posts since Sep 2008
Reputation Points: 10
Solved Threads: 0
 

I think you want to declare testString as static. That way every instance of the class will reference the same instance of testString.

Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

hi ancient dragon,
thanks for your reply, the thing is i want to create alot of instances of the class but i only want 2 of them to "share" the variable. so setting it as static wont work i think.

perhaps what i want is not possible but i dont know!!

cheers,
mono

monogana
Newbie Poster
12 posts since Sep 2008
Reputation Points: 10
Solved Threads: 0
 

try this out

class MyClass
{
public:
	MyClass(int& xx) : x(xx){};

private:
	int& x;
};

int main()
{
	int x;
	MyClass  c(x);
}
Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

thank you,
thats great......

you have really helped me

mono

monogana
Newbie Poster
12 posts since Sep 2008
Reputation Points: 10
Solved Threads: 0
 

just realized, this only works when construcing the class is there a way after the class is created?

i.e
int x = 200;
int z = 400;
MyClass a(x);
MyClass b(z);

// now i want b.x to share the address of a.x

cheers,
mono

monogana
Newbie Poster
12 posts since Sep 2008
Reputation Points: 10
Solved Threads: 0
 

AFAIK Must be done in the constructor.

Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You