Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
assigning the address of a variable in c++
Options
-
01-09-2008 1:12amhi 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 soclass 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 functions to have the same addresses
is this possible or is there a better way to do this??
thanks
mark0
Comments
-
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.
shouldn't that be:int a = 10; int b = &a;
not too sure on your class problem as it's been quite some time since i did C++ and i'm very rusty.0 -
hey cremo,
im pretty sure its
int a = 10;
int & b (a);
try this#include <string> #include <iostream> using namespace std; int main() { int a = 10; int &b(a); cout << "Before\ta: " << a << "\t" << "b: " << b << endl; a = 230; cout << "After\ta: " << a << "\t" << "b: " << b << endl; cout << "Addresses a: " << &a << "\t" << "b: " << &b << endl; cin.get(); return 0; }
0 -
Hey Mark,
What you want is doable alright. Was this an exercise its designed to get you thinking about the difference between pointers, references and local variables? In your testClass, testString is just a member variable of type string. If you think about how this class is laid out in memory your going to have...
<int> - 4 bytes
<string> - 32 bytes, this is a string object
Important point is that the testString is not a pointer to a string class, it is an actual string object sitting there in your class. This isn't a pointer, which is probably what you want. If it was a pointer it could point to a string object created on the heap that >1 testClass objects reference.
If you changed your class definition to:class testClass { public: int testInt; string* testString; testClass() { testInt = 0; testString = NULL; } testClass(int& i, string* s) { testInt = i; testString = s; } };
Would that get you any closer?
Cheers,
-mem0 -
hi memorex,
thanks for your input, its not an exercise - its something i want to be able to do - i am not doing a course or anything , just reading a book and diving in with goals to achieve (create useful programs - useful for me anyway)
this solution i already had, here is the problem - i dont want to assign the string as a point in the construtor - i want it assigned after the class intstance is created -
do you know if this is possible??
so for example#include <iostream> using namespace std; class MyClass { public: int testInt; string* testString; MyClass() {testInt = 0; testString = NULL; } MyClass(int& i, string* s) { testInt = i; testString = s; } }; int main() { string myString = "blah"; MyClass a; MyClass b; a.testInt = 100; b.testInt = 50; a.testString = &myString; // THIS DOESNT WORK cout << "a.testInt: " << a.testInt << "\tb.testInt: " << b.testInt << endl; cout << "a: " << &a.testString << "\tb: " << &b.testString << endl; cout << "a.testString: " << a.testString << "\tb.testString: " << b.testString << endl; bool isInstance = &a.testString == &b.testString; cout << "isInstance: " << isInstance << endl; cin.get(); return 0; }
thanks,0 -
What you are looking for is a STATIC class variable.
class testClass { public: int testInt; [b]static[/b] string testString; testClass() : testInt(0), testString("") {} testClass(int& i, string& s) { testInt = i; testString = s; } };
Then to get/set the variable outside of the class reference it as follows:
testClass::testString = "Whatever!"
Every instance of testClass will now have a testString of "Whatever!"0 -
Advertisement
-
Hey Mark,
Gotcha - good stuff re. learning c++ yourself.
What your use case has touched on here is a key part of the language and if you can get your head around it, the quicker things will start making sense.
Where you have:
&a.testString
This is the address of the member variable testString in 'a'.
&b.testString
Correspondingly, this is the address of the member variable testString in 'b'.
These are not going to be the same. That is because a and b are instances of the class MyClass. Each instance is laid out in memory and has its own address. In each instance the testString member will have a different address, so comparing these will fail. However by assigning myString to each member variable, they will both point to the same string.
Essentially you are pretty much done with your example, you just need to test the right things. Check out the list below. This hopefully will make sense.#include "stdafx.h" #include <iostream> using namespace std; int _tmain(int argc, _TCHAR* argv[]) { class MyClass { public: int testInt; string* testString; MyClass() {testInt = 0; testString = NULL; } MyClass(int& i, string* s) { testInt = i; testString = s; } }; string myString = "blah"; MyClass a; MyClass b; a.testInt = 100; b.testInt = 50; a.testString = &myString; // THIS DOESNT WORK, this does work b.testString = &myString; // you just point b.testString to the same thing cout << "a.testInt: " << a.testInt << "\tb.testInt: " << b.testInt << endl; cout << "start of a in mem: " << &a << endl; // the base address of a cout << "a testString: " << &a.testString << endl; // the address of the member variable testString in a cout << "start of b in mem: " << &b << endl; // the base address of b cout << "a testString: " << &b.testString << endl; // the address of the member variable testString in b // print out the contents of each testString cout << "a.testString: " << a.testString->c_str() << "\tb.testString: " << b.testString->c_str() << endl; // check if they are the same thing - note that == is overloaded for string bool isInstance = ((a.testString) == (b.testString)); cout << "isInstance: " << isInstance << endl; // update the shared string myString.append(" blah"); // test the result again cout << "a.testString: " << a.testString->c_str() << "\tb.testString: " << b.testString->c_str() << endl; cin.get(); return 0; }
0 -
hi mem,
wow thats great,
thanks so much for your help.........
working as i wanted.....#include <tchar.h> #include <iostream> using namespace std; int _tmain(int argc, _TCHAR* argv[]) { class MyClass { public: int testInt; string* testString; MyClass() {testInt = 0; testString = NULL; } MyClass(int& i, string* s) { testInt = i; testString = s; } }; string myString = "blah"; MyClass a; MyClass b; a.testInt = 100; b.testInt = 50; a.testString = &myString; // THIS DOESNT WORK, this does work //b.testString = &myString; // you just point b.testString to the same thing b.testString = a.testString; cout << "a.testInt: " << a.testInt << "\tb.testInt: " << b.testInt << endl; cout << "start of a in mem: " << &a << endl; // the base address of a cout << "a testString: " << &a.testString << endl; // the address of the member variable testString in a cout << "start of b in mem: " << &b << endl; // the base address of b cout << "b testString: " << &b.testString << endl; // the address of the member variable testString in b // print out the contents of each testString cout << "a.testString: " << a.testString->c_str() << "\tb.testString: " << b.testString->c_str() << endl; string diffString = "bleet"; *(a.testString) = "bleet"; *(b.testString) = "done"; cout << "a Address: " << &a.testString << "\t" << "b Address: " << &b.testString << endl; cout << "a.testString: " << a.testString->c_str() << "\tb.testString: " << b.testString->c_str() << endl; // check if they are the same thing - note that == is overloaded for string bool isInstance = ((a.testString) == (b.testString)); cout << "isInstance: " << isInstance << endl; // update the shared string //myString.append(" blah"); // test the result again cout << "a.testString: " << a.testString << "\tb.testString: " << b.testString << endl; cin.get(); return 0; }
Mr.StRiPe hi,
thanks for your reply, unfortunatly i wasnt looking for a static, i want to dynamically assign the value possibly multiple times, and needs to be different in every class instance
but thanks for the input, mem gave the answer i was looking for!!0
Advertisement