|
I would say the copy constructor
anyway its better to store a pointer to your class
try:
vector<test*> hej;
hej.push_back(&t);
Papa
while (TRUE)
Papa.WillLove ( Bebe ) ;
|
|
|
|
|
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...
|
|
|
|
|
if it will goes out of scope just new it first
Papa
while (TRUE)
Papa.WillLove ( Bebe ) ;
|
|
|
|
|
Why did you want to use a pointer in the first place? What is the advantage?
|
|
|
|
|
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 ?
|
|
|
|
|
Using pointer will settle this for you.
if you dont want to having a copy constructor will do
Papa
while (TRUE)
Papa.WillLove ( Bebe ) ;
|
|
|
|
|
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)
|
|
|
|
|
Do you have all the necessary #include s in place?
"The pointy end goes in the other man." - Antonio Banderas (Zorro, 1998)
|
|
|
|
|
I have:
#include <afxwin.h>
#include <vector>
using namespace std;
|
|
|
|
|
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<test> hej;
hej.push_back(t);
}
"The pointy end goes in the other man." - Antonio Banderas (Zorro, 1998)
|
|
|
|
|
Yeah, that seems to work.. Thank you(and Navin)
|
|
|
|
|
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.
|
|
|
|
|
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
|
|
|
|
|
Thank you, Paul, that was a good answer.
|
|
|
|
|
Paul 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
|
|
|
|
|
Hi all,
I'm having what I consider to be an inexplicable problem printing in my program. I've searched message boards, google, etc.. for the past week and no dice. I have two different "reports" that are displayed in the main window of my SDI app and should be printable. All of the drawing code is contained in OnDraw(), and the program draws the appropriate report based on flags set by button presses. Both reports display fine in Print Preview, but only one will actually print. When i print the other, the printer just spits out a blank page. I've replaced the drawing code in the function to simply display a rectangle in the center of the view for testing, but it still displays fine in print preview, but wont print. The report that is displayed by default is the one that prints okay, so i'm wondering if I need to re-initialize the CDC between prints or something along those lines. Thanks for reading this far, and thanks for any help you can provide.
|
|
|
|
|
Acetate wrote:
i'm wondering if I need to re-initialize the CDC between prints...
Printing the non-working report first will tell you if it's an initialization issue or not.
"The pointy end goes in the other man." - Antonio Banderas (Zorro, 1998)
|
|
|
|
|
True true. Just tried that, and sure enough, its still shooting out a blank page. Thanks for responding.
|
|
|
|
|
I'm sorry I could not be of more help. A lot of times, knowing what the problem is not is just as important as knowing what the problem is.
Thomas Edison said something to the effect of "I have not failed. I've just found 10,000 ways that won't work."
"The pointy end goes in the other man." - Antonio Banderas (Zorro, 1998)
|
|
|
|
|
Do you have both reports in the SDI view or do you switch between the 2 reports? Do you print both reports on 2 pages or only one?
If you do something more complex the MFC classes often won't do as they should. I think that I can say that I have stressed the MFC classes to their limits with the printing in my project...
I would try to implement the CView::OnPrint method and try to draw your content not directly to the print DC but first into a memory DIB which is compatible to the screen. Then you have to have to use StretchDIBits to "copy" the DIB to the printer. My experience is that in general it is not a good idea to do the output directly on the printer with the same drawing code which you use for your screen output. Many printers support only a subset of the GDI functions which you can use to draw on the screen. Often the output looks ugly or you won't have any ouput. Since the PrintPreviw uses a compatible screen DC to draw (the print DC is only used to do the sizing) your output may look good on print preview but not on the printer. But there are also some cases where your output sucks in print preview but looks good on the printer.
If you dont't have experience with DIBs take a look at the MSDN knowledge base article "DIBs and Their Use" from Ron Gery.
So I would do something like this:
<br />
void CReportView::OnPrint(DCD* pDC)<br />
{<br />
OnDraw(&memoryDC);<br />
}<br />
Since I don't know your implementation this is just a hint how I would do it. But I think drawing in a DIB may solve your problem.
|
|
|
|
|
Hey,
I'm glad I'm not the only one who's encountered problems with the MFC printing classes. I appreciate the lengthy response. I switch between the 2 reports in the SDI view, and I print the reports seperately on two different pages. (They are only one page reports as well). I'll take a look at the DIB functions and get up to speed. The only thing is, I don't use any different GDI functions in the two reports, and the first one prints fine. Thanks again for taking the time to help.
|
|
|
|
|
OK, if you use the same GDI functions, than it seems to be an other problem. What are your configurations in the OnBeginPrinting and OnPrepareDC methods? Have you overwritten those methods? Do you modify the CPrintInfo object, e.g. you can set the number of pages to print.
|
|
|
|
|
I've been looking for an easy way to encrypt data with an API function but all I found - e.g DPAPI - use machines & users credentials and so on whereas I only want to provide a password to retreive the original data from any users and any computer.
Any help would be appreciated.
Richard
Tout programme dont la fiabilité dépend de l'homme n'est pas fiable
|
|
|
|
|