ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
#include <cstdlib>
#include "CClass/CClass.h"
#include <iostream>
#include <set>
//using namespace std;
/*
*
*/
int main(int argc, char** argv) {
for (int i=0; i<10; i++) {
CClass classVariable(i);
std::cout << i << " - classVariable object created at address: " << &classVariable << " with private variable a = " << classVariable.getVar() << "\n";
}
std::set<CClass *> classSet;
int i;
for (i=0; i<10; i++) {
CClass * classVariable;
classVariable = new CClass(i);
std::cout << i << " - classVariable object created using NEW at address: " << &classVariable << " with private variable a = " << classVariable->getVar() << "\n";
classSet.insert(classVariable);
}
std::set<CClass *>::iterator it;
i = 0;
for (it = classSet.begin(); it != classSet.end(); it++) {
delete *it;
std::cout << i << " - classVariable object deleted.\n";
i++;
}
return 0;
}
It appears either way the objects, despite getting instantiated with the correct private member variables, are all being stored at the same address. Is there anyway to create 10 different objects all stored at 10 different addresses?
Code:
0 - classVariable object created at address: 0x7fffffffe990 with private variable a = 0
1 - classVariable object created at address: 0x7fffffffe990 with private variable a = 1
2 - classVariable object created at address: 0x7fffffffe990 with private variable a = 2
3 - classVariable object created at address: 0x7fffffffe990 with private variable a = 3
4 - classVariable object created at address: 0x7fffffffe990 with private variable a = 4
5 - classVariable object created at address: 0x7fffffffe990 with private variable a = 5
6 - classVariable object created at address: 0x7fffffffe990 with private variable a = 6
7 - classVariable object created at address: 0x7fffffffe990 with private variable a = 7
8 - classVariable object created at address: 0x7fffffffe990 with private variable a = 8
9 - classVariable object created at address: 0x7fffffffe990 with private variable a = 9
0 - classVariable object created using NEW at address: 0x7fffffffe988 with private variable a = 0
1 - classVariable object created using NEW at address: 0x7fffffffe988 with private variable a = 1
2 - classVariable object created using NEW at address: 0x7fffffffe988 with private variable a = 2
3 - classVariable object created using NEW at address: 0x7fffffffe988 with private variable a = 3
4 - classVariable object created using NEW at address: 0x7fffffffe988 with private variable a = 4
5 - classVariable object created using NEW at address: 0x7fffffffe988 with private variable a = 5
6 - classVariable object created using NEW at address: 0x7fffffffe988 with private variable a = 6
7 - classVariable object created using NEW at address: 0x7fffffffe988 with private variable a = 7
8 - classVariable object created using NEW at address: 0x7fffffffe988 with private variable a = 8
9 - classVariable object created using NEW at address: 0x7fffffffe988 with private variable a = 9
0 - classVariable object deleted.
1 - classVariable object deleted.
2 - classVariable object deleted.
3 - classVariable object deleted.
4 - classVariable object deleted.
5 - classVariable object deleted.
6 - classVariable object deleted.
7 - classVariable object deleted.
8 - classVariable object deleted.
9 - classVariable object deleted.
To make it more obvious what is happening, use three loops. The first one builds the objects. The second one (new...) loops through the collection and prints the variable. The third one destroys the objects.
The reason you are getting the same address is that you are actually printing out the address of the variable holding the address of the class object. At least in your second loop, simply remove the & from the "&classVariable" compile and rerun. You will see different addresses.
For the first loop you will probably need to take your class variable and convert it into a void pointer, then you can print out the contents of the pointer (which should then be pointing to your class object).
Hi, am looking for a way to create a number of class objects in a loop.
In the first loop, each object is auto-destroyed at the end of scope of the for-loop block. I do not believe this is what you want. To preserve the existence of the object after you exit the loop, either allocate it on the heap or stuff a copy into a container (e.g. a std::vector) that is declared outside the loop. If you decide to allocate the object, then keep a reference to the address where the object is located. You can use an std::vector, or an std::set as you demonstrated in your code.
A simple example:
Code:
#include <iostream>
#include <vector>
class Foo
{
public:
Foo(int v) : val(v) {}
~Foo() {}
int getValue() const { return val; }
private:
int val;
};
int main()
{
std::vector<Foo*> collection;
for (int i = 0; i < 10; ++i)
{
collection.push_back(new Foo(i));
}
for (std::vector<Foo*>::iterator it = collection.begin(); it != collection.end(); ++it)
{
Foo* foo = *it;
std::cout << "Foo object at location " << foo << " contains value " << foo->getValue() << std::endl;
delete foo;
}
}
Hi, thanks everyone. First, definitely a bug in the second loop. As suggested I have removed the "&" operand and this is printing out the correct addresses as follow in the output. However, as observed in the output also, the first loop is still not working.
Code:
0 - classVariable object created at address: 0x7fffe6577470 with private variable a = 0
0 - classVariable object created using NEW at address: 0x5fe1060 with private variable a = 0
1 - classVariable object created using NEW at address: 0x5fe10b0 with private variable a = 1
2 - classVariable object created using NEW at address: 0x5fe1100 with private variable a = 2
3 - classVariable object created using NEW at address: 0x5fe1150 with private variable a = 3
4 - classVariable object created using NEW at address: 0x5fe11a0 with private variable a = 4
5 - classVariable object created using NEW at address: 0x5fe11f0 with private variable a = 5
6 - classVariable object created using NEW at address: 0x5fe1240 with private variable a = 6
7 - classVariable object created using NEW at address: 0x5fe1290 with private variable a = 7
8 - classVariable object created using NEW at address: 0x5fe12e0 with private variable a = 8
9 - classVariable object created using NEW at address: 0x5fe1330 with private variable a = 9
0 - classVariable object deleted.
1 - classVariable object deleted.
2 - classVariable object deleted.
3 - classVariable object deleted.
4 - classVariable object deleted.
5 - classVariable object deleted.
6 - classVariable object deleted.
7 - classVariable object deleted.
8 - classVariable object deleted.
9 - classVariable object deleted.
dwhitney67, understand perfectly what you are suggesting. However, I'm printing out the addresses within the first loop and within the same iteration. Any idea why this is still using the same address? Also, I've tried doing what you said anyway to put the objects into a std::set. Following is the revised code. However, not sure why I keep on getting an error message during compile time. Any idea what's happening here?
main.cpp:27: error: passing ‘const CClass’ as ‘this’ argument of ‘int CClass::getVar()’ discards qualifiers
Code:
std::set<CClass> classSet0;
std::set<CClass>::reverse_iterator classSet0Iterator;
int i;
for (int i=0; i<10; i++) {
CClass classVariable(i);
classSet0.insert(classVariable);
classSet0Iterator = classSet0.rbegin();
std::cout << i << " - classVariable object created at address: " << classSet0Iterator << " with private variable a = " << classSet0Iterator->getVar() << "\n";
}
dwhitney67, that isn't the cause. The class was written as follow so there isn't a const keyword there for the getVar() function.
I tried doing what you suggested anyway by using a const interator and it still throws the same error. Any idea?
dwhitney67, that isn't the cause. The class was written as follow so there isn't a const keyword there for the getVar() function.
I tried doing what you suggested anyway by using a const interator and it still throws the same error. Any idea?
I was wrong to suggest that a const iterator was necessary; apparently it is not. However, when I attempted to conjure up a simple example, I found that declaring getVar() as const was necessary; otherwise I got the following compilation error:
Code:
g++ Foo.cpp
Foo.cpp: In function ‘int main()’:
Foo.cpp:35:88: error: no matching function for call to ‘Foo::getVar() const’
Foo.cpp:35:88: note: candidate is:
Foo.cpp:11:9: note: int Foo::getVar() <near match>
Foo.cpp:11:9: note: no known conversion for implicit ‘this’ parameter from ‘std::reverse_iterator<std::_Rb_tree_const_iterator<Foo> >::pointer {aka const Foo*}’ to ‘Foo*’
To correct the compiler problem, I had to declare getVar() as const. Here's the code:
Code:
#include <set>
#include <iostream>
class Foo
{
public:
Foo(int v = 0) : var(v) {}
Foo(const Foo& other) : var(other.var) {}
int getVar() const { return var; }
bool operator<(const Foo& other) const
{
return var < other.var;
}
private:
int var;
};
int main()
{
std::set<Foo> fooSet;
for (int i = 0; i < 10; ++i)
{
Foo foo(i);
fooSet.insert(foo);
std::set<Foo>::reverse_iterator iter = fooSet.rbegin();
std::cout << i << " - foo object created with variable var = " << iter->getVar()
<< std::endl;
}
}
P.S. The address of the (Foo) object that is created in the loop is not very relevant. The object gets destroyed at the end of the loop. The std::set contains a copy of this object.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.