2016-04-21 8 views
1

Die Idee meines Programms war es, eine lokale Variable zu erstellen und so Konstruktor aufgerufen wird. Wenn die Variable den Gültigkeitsbereich verlassen hat, wird der Destruktor aufgerufen. Die ganze Arbeit wurde in Ctor und Dtor gemacht. Grundsätzlich wollte ich eine lokale Variable erstellen, die mir nicht wirklich wichtig ist und sie automatisch zerstören lassen.GCC-Dokumentation dieser Art von Deklaration int (a)

Dann würde ich so etwas tun:

typedef Foo DoInCtor 

Danach habe ich so etwas tun würde:

DoInCtor() 

, dass diese lokale var schaffen würde, dass ich nicht wirklich zu tun kümmern.

Dies ist etwas ähnlich zu dem, was ich gerade arbeitete:

#include <stdio.h> 
#include <typeinfo> 
#include <iostream> 

class Bar 
{ 
public : 
    Bar() {printf("BAR, Default Ctor %p\n", this);} 

    ~Bar() {printf("BAR, Default Dtor %p\n", this);} 
}; 

class Foo 
{ 
public : 
    Bar m_bar; 
    Foo() : m_bar(Bar()) {printf("FOO, Default Ctor %p\n", this);} 
    Foo(Bar ref_bar) : m_bar(ref_bar) {printf("FOO, Other Ctor %p", this);} 
    ~Foo() {printf("FOO, Default Dtor %p\n", this);} 
}; 

Foo foo; 

int main() 
{ 
    Bar bar1; 
    printf("bar1 address, outside local scope : %p\n", &bar1); 
    std::cout << "Type of bar1 : " << typeid(bar1).name() << std::endl; 
    // This is some lcoal scope 
    { 
    Foo(bar1); 
    printf("bar1 address, in local scope : %p\n", &bar1); 
    std::cout << "Type of bar1 : " << typeid(bar1).name() << std::endl; 
    } 
    Foo foo1= Foo(bar1); 
    printf("foo1 address, in local scope : %p\n", &foo1); 
    return 0; 
} 

Das gibt das folgende auf meinem Rechner, zusammengestellt mit g ++ 4.8.4 auf Ubuntu 14.04:

BAR, Default Ctor 0x6021f1 
FOO, Default Ctor 0x6021f1 
BAR, Default Ctor 0x7ffe6808cfcd 
bar1 address, outside local scope : 0x7ffe6808cfcd 
Type of bar1 : 3Bar 
BAR, Default Ctor 0x7ffe6808cfcf 
FOO, Default Ctor 0x7ffe6808cfcf 
bar1 address, in local scope : 0x7ffe6808cfcf 
Type of bar1 : 3Foo 
FOO, Default Dtor 0x7ffe6808cfcf 
BAR, Default Dtor 0x7ffe6808cfcf 
FOO, Other Ctor 0x7ffe6808cfceBAR, Default Dtor 0x7ffe6808cfcf 
foo1 address, in local scope : 0x7ffe6808cfce 
FOO, Default Dtor 0x7ffe6808cfce 
BAR, Default Dtor 0x7ffe6808cfce 
BAR, Default Dtor 0x7ffe6808cfcd 
FOO, Default Dtor 0x6021f1 
BAR, Default Dtor 0x6021f1 

Was mich stört ist, dass im Hauptfunktionsbereich bar1 ein Bar Typ ist, aber im lokalen Bereich ist es Foo Typ. Es scheint mir, dass dies eine Art von var-Deklaration ist. ich sogar versucht, im Anschluss an gcc:

int(a) 

und es erstellt eine Variable a die int Typ ist.

Die Frage ist hier, ob jemand mir eine Art von Dokumentation zur Verfügung stellen kann, wo dies im Detail erklärt wird. Irgendein GCC oder irgendeine andere Dokumentation.

+2

Und warum der Tag C? –

+0

Dies ist nur Standard C++. Sie dürfen Klammern um den Bezeichner in einer Deklaration setzen. Ist das alles, was du fragst? – davmac

+0

Ja, das war, was ich gefragt habe, wenn das Standard ist. Ich wollte nur wissen, ob es eine Dokumentation darüber gibt? –

Antwort

4

Sie erstellen nur eine lokale Variable mit dem Namen bar1, die die von main überschreibt und den Typ Foo hat. Sie erstellen keine temporäre Foo-Variable mit dem zweiten Konstruktor. Und in diesem Fall darf der Name in Klammern stehen.

Die verdächtige Linie ist identisch mit Foo bar1;

Verwandte Themen