|
Best I can tell from MSDN the constructor is calling uninitialized_fill_n a few levels down.
The remarks in MSDN say: This algorithm allows the decoupling of memory allocation from object construction.
Could be there way of saying it does a deep copy of one instance of an object and replicates it N times, which would explain the behavior your experiencing.
- S
50 cups of coffee and you know it's on!
|
|
|
|
|
|
See Stephen's response below. Basically your object is created once, then copied multiple times. Seems like kind of odd behavior to me....
- S
50 cups of coffee and you know it's on!
|
|
|
|
|
That's an interesting question! See if this code helps answer it.
#include <iostream>
#include <vector>
#include <stdlib.h>
using namespace std;
class Foo {
private:
int num;
public:
Foo() {
num = rand();
cout<<"Foo::Foo() - num = "<<num<<endl;
}
explicit Foo(const Foo& f) {
num = f.num;
cout<<"Foo::Foo(const Foo&) - num = "<<num<<endl;
}
const Foo& operator=(const Foo& f) {
cout<<"Foo::operator=()"<<endl;
num = f.num;
return *this;
}
int getnum() {
return num;
}
};
int main() {
vector<Foo> v(5);
cout<<"size = "<<v.size()<<endl;
cout<<"capacity = "<<v.capacity()<<endl;
vector<Foo>::iterator it = v.begin();
while( it != v.end() )
cout<<(*it++).getnum()<<endl;
return 0;
} This is the output I get on VC++ 2008:
Foo::Foo() - num = 41
Foo::Foo(const Foo&) - num = 41
Foo::Foo(const Foo&) - num = 41
Foo::Foo(const Foo&) - num = 41
Foo::Foo(const Foo&) - num = 41
Foo::Foo(const Foo&) - num = 41
size = 5
capacity = 5
41
41
41
41
41 As you can see, the copy constructor gets called 5 times! So the compiler first creates a temporary "Foo" instance and then copies it 5 times.
--
gleat
http://blogorama.nerdworks.in[ ^]
-- Once we figured out that taking our shoes and socks off would double our counting ability the technical glitch was quickly rectified. -- Chris Maunder, from the CP newsletter
|
|
|
|
|
There is no bug, you just aren't logging enough! Try this code:
#include "stdafx.h"
#include <iostream>
#include <vector>
using namespace std;
class Log
{
public:
Log()
{
cout << "Log()" << endl;
}
Log(const Log ©_me)
{
cout << "Log(const Log ©_me)" << endl;
}
~Log() { cout << "~Log()" << endl; }
};
int main()
{
vector<Log> g_Log(10);
return 0;
}
The output is:
Log()
Log(const Log ©_me)
Log(const Log ©_me)
Log(const Log ©_me)
Log(const Log ©_me)
Log(const Log ©_me)
Log(const Log ©_me)
Log(const Log ©_me)
Log(const Log ©_me)
Log(const Log ©_me)
Log(const Log ©_me)
~Log()
~Log()
~Log()
~Log()
~Log()
~Log()
~Log()
~Log()
~Log()
~Log()
~Log()
Looks like a reference element is created using the default constructor and the copy constructor is used to construct the actual vector 's elements by copying the reference element.
Steve
modified on Thursday, April 3, 2008 12:23 AM
|
|
|
|
|
|
Your mistake was assuming that the default constructor is used to construct the vector 's contents. My example showed that the copy-constructor is used instead. As I said, there is no problem: your vector does contain 2 fully constructed elements, it's just that you didn't put any logging code in the copy-constructor.
Steve
|
|
|
|
|
That's what I was starting to think. Thanks! (C# is destroying my brain!!)
- S
50 cups of coffee and you know it's on!
|
|
|
|
|
I haven't experienced the pleasure/pain (I’ll leave the choice up to the reader's discretion) of using C# yet.
Steve
|
|
|
|
|
You're a lucky man!
You just think differently (or rather don't have to think) about things in c#. After a year long break from c++, I looked at the stl vector class and it looked foreign and assembly like. That scares the crap out me! Gotta get back to my roots, back to the metal, before I become akin to a VB developer (not that there's anything wrong with that )
- S
50 cups of coffee and you know it's on!
|
|
|
|
|
i have a dialog box which can be resized...
now this can be resized only by dragging the bottom right side... is is possible?
i believe that there are three possibilities.one is to drag sideways another one is to drag downwards (if we do either of the above methods the video will be stretched) and the other one is to use the bottom right part...(i should be able to do only this so the aspect ratio of the video is not lost.
requirement is that the video should look stretched. how do i do this?
|
|
|
|
|
|
yes its on a control.. i am using directshow for this..
|
|
|
|
|
first of all if you see Mediaplayer the main window can be sized anyway, but the video window adjust to keep the aspect ratio of the video. So adjust the display of video than the dialog box, because size and aspect ratio varies for each video.
Chandrasekharanp wrote: now this can be resized only by dragging the bottom right side... is is possible?
you can prevent changing the size only in one dimension by overriding WM_SIZING, i cannot thing of any other advanced tricks.
Chandrasekharanp wrote: i believe that there are three possibilities.
There are more possibilities, WM_SIZING[^]
void CYourDlg::OnSizing(UINT fwSide, LPRECT pRect)
{
CDialog::OnSizing(fwSide, pRect);
if (fwSide != WMSZ_BOTTOMLEFT &&
fwSide != WMSZ_BOTTOMRIGHT &&
fwSide != WMSZ_TOPLEFT &&
fwSide != WMSZ_TOPRIGHT )
{
RECT rc;
GetWindowRect(&rc);
*pRect = rc;
}
}
|
|
|
|
|
i am using directshow here. and i have seen in media player that the aspect ratio does not change when u drag the window.. do we have an option in direct show to set the aspect ratio. there is one option available
<br />
[ nAspectRatio = ] MSWebDVD.AspectRatio <br />
i actually dint understand what this is.. i guess u have some knowledge on direct show.
|
|
|
|
|
Chandrasekharanp wrote: i am using directshow here
not purely, you are using an ActiveX control that build on Directshow filters.
Chandrasekharanp wrote: [ nAspectRatio = ] MSWebDVD.AspectRatio
Chandrasekharanp wrote: i actually dint understand what this is
I never used this control, this is the property that gives a number (some naming convention n-any number, i-interger, 0 -object, not very sure) it actually gives the aspect ratio of the video stream. And this is a read-only property so you cannot expect to do something like resizing video.
Try MSWebDVD.SetClipVideoRect(oRect); where you have to calculate the rectangle every time the dialog is resized (I guess you are resizing the control when dialog is resized, still need to be done) keeping the aspected ratio (aspect ratio of the video can be obtained from the property, MSWebDVD.AspectRatio) same.
* on size handler of dialog
* resize the control
* from the new size of control and the aspect ratio of video calculate video rect
* set the video Rect MSWebDVD.SetClipVideoRect().
Try this.
modified on Wednesday, March 26, 2008 3:42 AM
|
|
|
|
|
Chandrasekharanp wrote: i have a dialog box which can be resized...
Hi, how did you made your dialog resizable?
Is it dialog based application or different?
Best Regards,
Suman
|
|
|
|
|
in the properties of the dialog there are options to make it resizable (its a combobox in styles tab)..
and it is a dialog based application.
|
|
|
|
|
Hi,
I know that if i do this; program1.exe > out.txt, all the output of program1.exe will be written to out.txt file (printf, cout).
But, what happens when i do program1.exe < input.txt? Imagine input.txt contains some data, how would i load the data into program1.exe with the above command? What C++ code should i write to handle that?
Also, if i do program1.exe < input.txt, the argc in main() will only return 1, that is program1.exe. Why not 3? (for program1.exe, <, and input.txt)
Explanantion appreciated. Thanks.
(I'm using win32 console app on VC++)
|
|
|
|
|
uus831 wrote: But, what happens when i do program1.exe < input.txt?
stdin is changed from the keyboard to input.txt
To read the data you can use anything that reads from stdin line scanf(), getch() etc
uus831 wrote:
Also, if i do program1.exe < input.txt, the argc in main() will only return 1, that is program1.exe. Why not 3? (for program1.exe, <, and input.txt)
Because the < input.txt is not an argument to your application, its an instruction to the os to modify what stdin points to
|
|
|
|
|
Note that stdin and stdout are also file handles. When no redirection occurs, stdin is the keyboard and stdout is the monitor.
In the form of program1.exe > out.txt , stdout is assigned with a file handle to the file out.txt, instead of the monitor. So all the output goes into out.txt instead of the monitor.
In the form of program1.exe < in.txt , stdin is assigned with a file handle to the file in.txt, instead of the keyboard. So whenever you read from stdin, e.g. when you call the scanf() function, the data come form in.txt, not the keyboard.
Redirecting is handled by the OS, not by the program, so the program itself will know nothing about the redirection, and redirection directives would be passed to program as parameters. Thus argc in main is 1, not 3.
|
|
|
|
|
Thanks for the info. But how do i determine how much data to read from the file? I mean, if the file in.txt contains 10 rows of data, how can i determine that i need to call scanf 10 times? Or, i cant?
|
|
|
|
|
uus831 wrote: But how do i determine how much data to read from the file? I mean, if the file in.txt contains 10 rows of data, how can i determine that i need to call scanf 10 times? Or, i cant?
To read file, I suggest you not to use DOS-shell indirection (< ) but use program arguments instead. Such as:
MyTest.exe -i "sample.txt"
or
MyTest.exe /i "sample.txt"
In the above example, your application handles the command-line arguments and gets the file name(s). Then you can use fstream to load the content of the whole file(s), or to read part of the file(s).
Maxwell Chen
|
|
|
|
|
Ok, thanks man. That's a better idea.
|
|
|
|
|
As the Windows built-in tool, fc.exe , it compares two files. The command for using this tool is as:
fc /b C:\Temp\hello.txt D:\Doc\hello_2.txt
Maxwell Chen
|
|
|
|