MFC classes in my class
-
How smart is it to store a pointer if the object you added to the vector goes out of scope? I know that isn't the case right here, but generally speaking...
pie wrote: How smart is it to store a pointer if the object you added to the vector goes out of scope? If the object was allocated on the heap, it won't go out of scope per se. It will still exist until
delete
is called. In other words:void MyClass::SomeFunction( void )
{
SomePtr *ptr = new SomePtr;
m_vector.Add(ptr);
}Even though
ptr
goes out of scope, the memory that it points to is still alive and well.
"The pointy end goes in the other man." - Antonio Banderas (Zorro, 1998)
-
Hello. When I write something like: --- class test{ public: CBrush f; }; int main() { test t; vector hej; hej.push_back(t); } --- I get an error in : ...\include\vector(575): error C2440: 'initializing' : cannot convert from 'const test' to 'test' But if I replace CBrush with one of my own classes, i everything works fine! What is it with those MFC classes?
Do you have all the necessary
#include
s in place?
"The pointy end goes in the other man." - Antonio Banderas (Zorro, 1998)
-
For various reasons: Parameter passing to a function of objects is always better by pointer or by reference, the copy time is smaller and faster. and the allocator of your vector is faster too because you wouldn't reallocate data or allocate chunks often. and much more too like copy modifications ... Papa while (TRUE) Papa.WillLove ( Bebe ) ;
-
Do you have all the necessary
#include
s in place?
"The pointy end goes in the other man." - Antonio Banderas (Zorro, 1998)
-
For various reasons: Parameter passing to a function of objects is always better by pointer or by reference, the copy time is smaller and faster. and the allocator of your vector is faster too because you wouldn't reallocate data or allocate chunks often. and much more too like copy modifications ... Papa while (TRUE) Papa.WillLove ( Bebe ) ;
-
But i'm not looking for speed-improvements. I just want to know why the code i posted doesn't work. MUST I use pointers to make it work ?
-
Hello. When I write something like: --- class test{ public: CBrush f; }; int main() { test t; vector hej; hej.push_back(t); } --- I get an error in : ...\include\vector(575): error C2440: 'initializing' : cannot convert from 'const test' to 'test' But if I replace CBrush with one of my own classes, i everything works fine! What is it with those MFC classes?
You probably need to define a copy constructor for your class. E.g.,
class test { public: test(const test &src); CBrush f; }; test::test(const test &src) :f(src.f) { }
Then try it and see what happens. (Note, just a guess, I didn't actually try to compile this.) Sometimes I feel like I'm a USB printer in a parallel universe. -
No compiler errors with this:
class test
{
public:
CBrush f;
test();
test(const test& t);
~test();
const test& operator=(const test& t);
};void main( void )
{
test t;
vector hej;hej.push\_back(t);
}
"The pointy end goes in the other man." - Antonio Banderas (Zorro, 1998)
-
No compiler errors with this:
class test
{
public:
CBrush f;
test();
test(const test& t);
~test();
const test& operator=(const test& t);
};void main( void )
{
test t;
vector hej;hej.push\_back(t);
}
"The pointy end goes in the other man." - Antonio Banderas (Zorro, 1998)
-
Hello. When I write something like: --- class test{ public: CBrush f; }; int main() { test t; vector hej; hej.push_back(t); } --- I get an error in : ...\include\vector(575): error C2440: 'initializing' : cannot convert from 'const test' to 'test' But if I replace CBrush with one of my own classes, i everything works fine! What is it with those MFC classes?
Objects that can be stored in std:: containers must be copyable, and copies must be equivalent. This isn't the case with CBrush. Even if you arrange a copy constructor such that the object being copied to takes ownership,
test ( const test& t)
{
f.Attach ( t.f.Detach ()) ;
}you haven't met the 'equivalent' condition since the new object owns the Windows Brush and the old one no longer does. In your example if 't' was initialised with a valid brush after the
push_back
would no longer own it. The copy constructor above won't compile because the argument 't' is const and we're trying to modify it with 'Detach', sound familiar? In these circumstances it is appropriate to store a container of pointers and manage the destruction of the contained pointers manually. Paul -
Objects that can be stored in std:: containers must be copyable, and copies must be equivalent. This isn't the case with CBrush. Even if you arrange a copy constructor such that the object being copied to takes ownership,
test ( const test& t)
{
f.Attach ( t.f.Detach ()) ;
}you haven't met the 'equivalent' condition since the new object owns the Windows Brush and the old one no longer does. In your example if 't' was initialised with a valid brush after the
push_back
would no longer own it. The copy constructor above won't compile because the argument 't' is const and we're trying to modify it with 'Detach', sound familiar? In these circumstances it is appropriate to store a container of pointers and manage the destruction of the contained pointers manually. Paul -
Objects that can be stored in std:: containers must be copyable, and copies must be equivalent. This isn't the case with CBrush. Even if you arrange a copy constructor such that the object being copied to takes ownership,
test ( const test& t)
{
f.Attach ( t.f.Detach ()) ;
}you haven't met the 'equivalent' condition since the new object owns the Windows Brush and the old one no longer does. In your example if 't' was initialised with a valid brush after the
push_back
would no longer own it. The copy constructor above won't compile because the argument 't' is const and we're trying to modify it with 'Detach', sound familiar? In these circumstances it is appropriate to store a container of pointers and manage the destruction of the contained pointers manually. PaulPaul Ranson wrote: In these circumstances it is appropriate to store a container of pointers and manage the destruction of the contained pointers manually. Or even better, to use some smart pointers, like boost::shared_ptr or Loki::SmartPtr