c++ - Forward class declaration is interchangeable with the class keyword in the place of use? -


are these 2 equivalent?

code 1:

class b;  class { public:     b fun1() const;     b* m_b; };  extern void myfun( const b& b ); 

code 2:

class { public:     class b fun1() const;     class b* m_b; };  extern void myfun( const class b& b ); 

or there problematic points use programming style presented in code 2?

these different if have enclosing scope.

case 1:

class b { }; namespace test {     class b;  // declares test::b      class {     public:         b fun1() const; // refers test::b         b* m_b;         // refers test::b     };      class b { int x; };  // defines test::b } 

case 2:

class b { }; namespace test {     class {     public:         class b fun1() const; // refers ::b         class b* m_b;         // refers ::b     };      class b { int x; };  // defines test::b } 

more details:

class b; 1 of 2 things. it's either redeclaration, or forward declaration introduces name b current scope (§9.1 [class.name]/p2 of standard).

class b called elaborated-type-specifier. when use declare something, compiler first looks see if type called b in scope (§3.4.4 [basic.lookup.elab]/p2, §9.1 [class.name]/p3 note). if there is, elaborated-type-specifier taken refer that.

if not, declare new name. difference forward declaration in scope in new name declared. if used specify return type or parameter type of function of namespace scope, it's taken declare name in namespace contains declaration; otherwise, except friend declaration, name declared in smallest namespace or block scope contains declaration (§3.3.2 [basic.scope.pdecl]/p7). note never declares name in class scope; in other words, never declare nested class.

why use forward declarations instead of alternative? first, can declare name in scope, not block , namespace scope. second, forward declarations less fragile. note in second case if move definition of test::b before test::a class bs in test::a referring test::b rather ::b. third, want have remember entire paragraph of rules scope declared name put in?


Comments

Popular posts from this blog

google api - Incomplete response from Gmail API threads.list -

Installing Android SQLite Asset Helper -

Qt Creator - Searching files with Locator including folder -