Hypercell ein ] Hypercell aus ] Zeige Navigation ] Verstecke Navigation ]
c++.de  
   

Die mobilen Seiten von c++.de:
http://m.c-plusplus.de
Infos hier [BETA]

  
c++.de ::  C++ (auch C++0x und C++11) ::  Different forms of copy constructor
Antwort schreiben
Benutzername:
Titel:
Nachrichtentext:
  :)  :D  ;)  :(  :p  :mad:  :rolleyes:  :eek:  :confused:  :cool:  :o)  :leak:  :live:  :die:  :idea:  :arrow:  :warning: 
                             
                         
         
           
                             
                             
                             
             


BBCode in diesem Beitrag deaktivieren [BBCode]
Smilies in diesem Beitrag deaktivieren
Different forms of copy constructor and 306750
     


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. :D


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 :D
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 :D
Nö, da kommt kein to. Ich hab auch schon Leute getroffen, die seit 10 Jahren in Deutschland leben und kein deutsch können. :D
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. :D
Sone
21:50:17 27.08.2012   Titel:   Re: Different forms of copy constructor Zitieren

HaHa :D
Zitat:

Sone schrieb:

I dare you to say, VC++ is nearly as good as GCC is :cool: :D
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 :D
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. :D

;)
</scnr>


Haha :D
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 :cool: :D
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. :D

;)
</scnr>
Unregistrierter
20:28:36 27.08.2012   Titel:   Zitieren

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 :cool: :D
camper
15:10:05 27.08.2012   Titel:   Zitieren

yes
SAn
13:57:10 27.08.2012   Titel:   Zitieren

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
08:59:13 07.08.2012   Titel:   Zitieren

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
08:34:43 07.08.2012   Titel:   Zitieren

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.

Powered by phpBB © 2001, 2002 phpBB Group :: FI Theme

c++.de ist Teilnehmer des Partnerprogramms von Amazon Europe S.à.r.l. und Partner des Werbeprogramms, das zur Bereitstellung eines Mediums für Websites konzipiert wurde, mittels dessen durch die Platzierung von Werbeanzeigen und Links zu amazon.de Werbekostenerstattung verdient werden kann.

Die Vervielfältigung der auf den Seiten www.c-plusplus.de, www.c-plusplus.info und www.c-plusplus.net enthaltenen Informationen ohne eine schriftliche Genehmigung des Seitenbetreibers ist untersagt (vgl. §4 Urheberrechtsgesetz). Die Nutzung und Änderung der vorgestellten Strukturen und Verfahren in privaten und kommerziellen Softwareanwendungen ist ausdrücklich erlaubt, soweit keine Rechte Dritter verletzt werden. Der Seitenbetreiber übernimmt keine Gewähr für die Funktion einzelner Beiträge oder Programmfragmente, insbesondere übernimmt er keine Haftung für eventuelle aus dem Gebrauch entstehenden Folgeschäden.