Thanks Banfa, your reply was helpfull. But my doubt is that in the code, I am printing values of variables, which are not static. So, how come I am able to print their values, when I am actually not creating any object? Because, I think that the non-static variables of a class come into existance only when the object is created. ... |
Hi, This code is of no use. But I am just curious to know about what happening here. #include using namespace std; class Foo { private: int i; public: Foo() { cout << "Foo::Foo()" << endl; Foo(i); } Foo(int i) : i(i) { cout << "Foo::Foo(int)" << endl; } ~Foo() { cout << "Foo::~Foo()" << endl; } }; int main(int ... |
Its been awhile and I am rusty. Can the constructor of my class call another method in the same class if that other method does not change any member data? I want to simply have a seperate method that returns a huge string, that string containing code in another language, which is to be compiled by a third party API when ... |
AhmedB Hi, I wrote this program, compiled it and ran it with the expectation that I will get two printed lines (one from each ctor): #include class A { public: A(int a) { fprintf(stderr, "A(int a)\n"); i = a; } A(const A& a) { fprintf(stderr, "A(const A& a)\n"); i = a.i; } private: int i; private: const A& operator= (const ... |
Hi Could you please tell why copy constructor is not called in the first line in main(), as mentioned in the text books. I used g++ version 4.1.2 on debian etch thanks suresh # include using namespace std; class base { char s; public: base( ){cout<< "construction" << endl;} base(const char a ) {cout << "construction with arg" << endl;} ... |
Olers1, you're right. The copy constructor is what is missing for this example. If it is added, then the constructors and destructors match. Also, operator= defined in this example, doesn't return anything. You need to add that. Oddly, this doesn't cause a warning. Add these changes and your code should work as expected. |
//--------------------------------------------------------------------------------- TextureCoordRect::TextureCoordRect(D3DXVECTOR2 topLeft, D3DXVECTOR2 bottomRight) { if( topLeft.x < 0.0f || topLeft.x 1.0f || topLeft.y < 0.0f || topLeft.y 1.0f || bottomRight.x < 0.0f || bottomRight.x 1.0f || bottomRight.y < 0.0f || bottomRight.y 1.0f) throw Error("Attempting to create texture coordinate rect with invalid coordinates", "TextureCoordRect::TextureCoordRect(D3DXVECTOR 2 topLeft, D3DXVECTOR2 bottomRight)", "Font2D.cpp"); m_topLeft = topLeft; m_bottomRight = bottomRight; } //--------------------------------------------------------------------------------- TextureCoordRect::TextureCoordRect(float topLeft_u, float ... |
|
------------------- class A { public: A() { blabla(); } A(int a) : A() { another_blabla(); } // why wrong? } ------------------- I want 2nd ctor, runs content of 1st ctor and after its content. In Java, I must use in first body line of A(int a) this: ------------------- this(); ------------------- In C++? thanks |
* Kavya: Suppose I have class MyClass. Then which will be termed as explicit call to constructor. > MyClass( ); > or > MyClass object; object.MyClass( ); The second line of the latter isn't valid code. The first is an example of what the standard calls an explicit constructor call, while the first line of the second example is a declaration ... |
Consider the code below. The output is the following two lines: 0xbfc78090 0xbfc780a0 This proves that the variable m in main() is not the very same instance of MyClass as temp_m in hello(). Hence (?) m is created as copy of temp_m. But the copy constructor is not called. Contradiction. Where am I thinking incorrectly? #include using namespace std; class ... |
Szabolcs Horvt Consider the attached example program: an object of type 'A' is inserted into a 'map |
hi 1 #include 2 3 class A{ 4 private: 5 int x; 6 public: 7 A(int x){ 8 this->x; 9 } 10 11 A(int x, char *y){ 12 A(x); <---------------- may i know how to fix this line? 13 } 14 }; 15 16 int main(){ 17 return 1; 18 } d.cpp: In constructor `A::A(int, char*)': d.cpp:12: error: declaration of ... |
Frederick Gotham wrote: Andy posted: > public class MyClass { public MyClass() { this(10); } public MyClass(int someNumber) { ... } ... } > > I'd don't see any problem with the following: > #include > struct MyClass { public: > MyClass(double a, int b) { ::new(this) MyClass(a * 3 / b); } > MyClass(unsigned const i) { /* Code ... |
In the code below, at instantiation of a "listC" object, the constructors for attributes "a", "b", and "c" are called in sequence from "a" to "c". How much is this behaviour reliable? Does the c++ specifics tell something about it? What are the odds for the constructors to be called in a different sequence, like "cba" or "bca" or whatever? What ... |
I'm trying to write some code that calls the constructors of STL containers explicitly, and I can't get it to compile. A sample program is below. One compiler complains about the last two lines (the map constructor calls), saying that I'm trying to take the address of a constructor. Another compiler complains about all four of the last lines, just saying ... |
Not 100% sure, but I think it goes something like this: Yes, the copy constructor strictly should be called, but... Prepare() is creating a new stmt and the only place it's going is back to main. There's no point in making a copy since there needs to be only one. The s in prepare() drops out of scope at the same ... |
If a container class List has a copy constructor and the privately derived class Stack needs to call the Container class's copy constructor. How can I do it? List::List(const List &list) { *this = list; } Stack::Stack(const Stack &stack) { List(stack); //stack has the type Stack, it can't be converted into List, what can I do? } |
|