Themen-Überblick
(Aktualisieren)
| Autor |
Nachricht |
Sone
|
| 22:08:21 27.08.2012 Titel: |
Re: Different forms of copy constructor |
Zitieren |
| Gugelmoser schrieb: |
| Sone schrieb: | | bei mir kommt son Satz einfach rausgeschossen, ich denke gar nicht drüber nach. Es hört (und "fühlt") sich einfach richtig an... | Dein Abi Lehrer wird dir was husten.  |
Genau so hat mir das mein Mathe-Lehrer auch gesagt, als ich ihm so einen dirty hack vorgeführt habe, mit dem ich mir vor einem Jahr oder so Polynom-Division vereinfachen wollte  |
|
|
 |
Unregistrierter
|
| 21:51:06 27.08.2012 Titel: |
Re: Different forms of copy constructor |
Zitieren |
| Sone schrieb: | Nö, eigentlich sollte nach dare doch ein 'to'. Zumindest sagt es so mein Freund, und der ist 10 Jahre lang in Florida aufgewachsen... und spricht immer noch mehr Englisch als Deutsch... KA  | Nö, da kommt kein to. Ich hab auch schon Leute getroffen, die seit 10 Jahren in Deutschland leben und kein deutsch können.
edit: Ah ok, man kann ein to setzen, muss es aber nicht. Ich habe aber mal noch nie jemanden gehört, der da ein to benutzt.
| Sone schrieb: | | bei mir kommt son Satz einfach rausgeschossen, ich denke gar nicht drüber nach. Es hört (und "fühlt") sich einfach richtig an... | Dein Abi Lehrer wird dir was husten.  |
|
|
 |
Sone
|
| 21:50:17 27.08.2012 Titel: |
Re: Different forms of copy constructor |
Zitieren |
HaHa
| Zitat: |
| Sone schrieb: |
I dare you to say, VC++ is nearly as good as GCC is  | Meinst du nicht, dass sich dieser Satz etwas merkwürdig anhört? Was du eigentlich sagen wolltest ist: Don't you dare say (kein Gerund) VC++ is nearly as good as GCC. |
Nö, eigentlich sollte nach dare doch ein 'to'. Zumindest sagt es so mein Freund, und der ist 10 Jahre lang in Florida aufgewachsen... und spricht immer noch mehr Englisch als Deutsch... KA
| Zitat: |
| Sone schrieb: |
pumuckl already explained it pretty well.
| Im Allgemeinen ist die Regel korrekt (wie in diesem Fall): Wenn sich das Adjektiv auf ein Verb bezieht, musst du das Adverb nehmen. Es gibt aber auch Ausnahmen, wie z.B.
you look beautiful
sleep good
sleep tight
it smells good
...
Wann es eine Ausnahme ist und wann nicht, keine Ahnung ob es da eine Regel gibt, das lernt man aber automatisch, wenn man sich mit Englisch beschäftigt.
Und noch eine Information: well ist sowohl Adverb als auch Adjektiv. Sprich du kannst sagen I am well oder I am good. Beides ist korrekt, bedeutet aber nich dasselbe. Mittels well beziehst du dich auf deine körperliche Gesundheit und mittels good sagst du, wie es dir gerade emotional geht (also psychisch), z.B. wenn du sagen willst, dass du gerade glücklich bist.
Lustig wirds auch bei tight und tightly, das darst du aber selbst nachschlagen.
</scnr> |
Haha
Glaub mir, ich spreche sehr viel Englisch. Und bei mir kommt son Satz einfach rausgeschossen, ich denke gar nicht drüber nach. Es hört (und "fühlt") sich einfach richtig an... |
|
|
 |
Unregistrierter
|
| 21:28:02 27.08.2012 Titel: |
Re: Different forms of copy constructor |
Zitieren |
<scnr>
Sone darf jetzt noch etwas lernen.
| Sone schrieb: |
I dare you to say, VC++ is nearly as good as GCC is  | Meinst du nicht, dass sich dieser Satz etwas merkwürdig anhört? Was du eigentlich sagen wolltest ist: Don't you dare say (kein Gerund) VC++ is nearly as good as GCC.
| Sone schrieb: |
pumuckl already explained it pretty well.
| Im Allgemeinen ist die Regel korrekt (wie in diesem Fall): Wenn sich das Adjektiv auf ein Verb bezieht, musst du das Adverb nehmen. Es gibt aber auch Ausnahmen, wie z.B.
you look beautiful
sleep good
sleep tight
it smells good
...
Wann es eine Ausnahme ist und wann nicht, keine Ahnung ob es da eine Regel gibt, das lernt man aber automatisch, wenn man sich mit Englisch beschäftigt.
Und noch eine Information: well ist sowohl Adverb als auch Adjektiv. Sprich du kannst sagen I am well oder I am good. Beides ist korrekt, bedeutet aber nich dasselbe. Mittels well beziehst du dich auf deine körperliche Gesundheit und mittels good sagst du, wie es dir gerade emotional geht (also psychisch), z.B. wenn du sagen willst, dass du gerade glücklich bist.
Lustig wirds auch bei tight und tightly, das darst du aber selbst nachschlagen.
</scnr> |
|
|
 |
Unregistrierter
|
| C++: | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 | #include <iostream> struct MyInt { int x; MyInt(int const y): x( y ) { std::cout << "Constructor 1 called." << std::endl; } MyInt(MyInt const &y): x( y.x ) { std::cout << "Constructor 2 called." << std::endl; } }; int main() { /* So, what's going on there? In fact it is not that complicated: 1) A instance, named 'i', shall be created. 2) The right operand of the assignment operator is a literal of type 'int', thus your struct 'MyInt' must have a constructor looking like 'MyInt(int)'. 3) Well done, your struct 'MyInt' does have a constructor looking like 'MyInt(int)', thus the compiler is happy. 4) a) The constructor 'MyInt(int)' is called, creating a temporary. ( like 'MyInt tmp(1);' ) In theory: b) That created temporary serves as argument for the copy constructor 'MyInt(MyInt const&)'. c) The copy constructor receives that temporary. d) The copy constructor is now capable of initializing the actual instance 'i' with the aid of that temporary. --> Finally two instances were created, the temporary and 'i'. --> And to be more precise, that two instances are equivalent. --> Kinda stupid and absolutely unnecessary, isn't it? In practice: b) The copy constructor 'MyInt(MyInt const&)' is not called. The constructor 'MyInt(int)' is the only constructor being called. c) As a consequence, the instance created by 'MyInt(int)' is no temporary but the actual instance 'i'. --> Finally only one instance was created, namely 'i'. --> That's pretty cool, isn't it? --> NOTE: The theoretical process must be feasible (compilable), otherwise the practical process can't be done. What I'm trying to say is, you will get a compiler error, if the copy constructor is not public. */ MyInt i = 1; } | |
|
|
|
 |
Sone
|
| 19:19:45 27.08.2012 Titel: |
Re: Different forms of copy constructor |
Zitieren |
| SAn schrieb: |
So, the following behavior is standard compliant?
|
Why shouldn't it be? Thats simply another form of calling the corresponding ctor, known as "copy initialisation". pumuckl already explained it pretty well.
And btw: I dare you to say, VC++ is nearly as good as GCC is  |
|
|
 |
camper
|
|
 |
SAn
|
New information from man g++:
| Code: | -fno-elide-constructors The C++ standard allows an implementation to omit creating a temporary which is only used to initialize another object of the same type. Specifying this option disables that optimization, and forces G++ to call the copy constructor in all cases. | |
So, the following behavior is standard compliant?
| C++: | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | #include <iostream> class MyInt { private: int x; public: MyInt(int const y): x( y ) { std::cout << "Constructor 1 called." << std::endl; } MyInt(MyInt const &y): x( y.x ) { std::cout << "Constructor 2 called." << std::endl; } }; int main(void) { MyInt i = 1; return 0; } | |
Output: | Code: | | Constructor 1 called. | |
|
|
|
 |
pumuckl
|
| SAn schrieb: | | As you can see, Visual Studio is better, but GCC is more correct. | AFAIK, both are correct. For copy-initialization, the Copy-Ctor has to be available, but does not ned to be called i.e. may be omitted completely. |
|
|
 |
SAn
|
camper, thank you for detailed explanation. I was suspecting something like that.
Here is interesting example:
| C++: | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | #include <iostream> struct MyInt { int x; MyInt(int const y): x( y ) { std::cout << "Constructor 1 called." << std::endl; } MyInt(MyInt const &y): x( y.x ) { std::cout << "Constructor 2 called." << std::endl; } }; int main(void) { MyInt i = 1; return 0; } | |
GCC output:
| Code: | Constructor 1 called. Constructor 2 called. | |
Visual Studio output:
| Code: | | Constructor 1 called. | |
As you can see, Visual Studio is better, but GCC is more correct. |
|
|
 |
|
|
|
|
|