Local classes in C ++

I read the concept of "Local Classes" in object-oriented programming using C ++ By Balagurusamy ( http://highered.mcgraw-hill.com/sites/0070593620/information_center_view0/ ).

The last line indicates that the close function cannot access the private members of the local class. However, we can achieve this by declaring the closing function as a friend. "

Now I am wondering, how can I make the selected part?

Here is the code I tried but no luck,

#include<iostream> using namespace std; class abc; int pqr(abc t) { class abc { int x; public: int xyz() { return x=4; } friend int pqr(abc); }; t.xyz(); return tx; } int main() { abc t; cout<<"Return "<<pqr(t)<<endl; } 

I know that the code looks wrong, any help will be noticeable.

+4
source share
4 answers

Your friend statement is fine.

 int pqr() { class abc { int x; public: abc() : x(4) { } friend int pqr(); }; return abc().x; } int main() { cout << "Return " << pqr() << endl; } 

Edit:
IBM offers this explanation for the issue raised in the comments:

If you declare a friend in a local class and the friend’s name is unqualified, the compiler will only search for the name inside the innermost enclosing scope of the nonclass. [...] You do not need to do this with classes.

 void a(); void f() { class A { // error: friend declaration 'void a()' in local class without prior decl... friend void a(); }; } 

friend void a (): This statement does not consider the function a () declared in the namespace scope. Since the function a () was not declared in the scope of f (), the compiler would not allow this statement.

Source: IBM - Friend Domain (C ++ only)

So you are out of luck. Balagurusamy only works for MSVC and similar compilers. You can try passing the execution to the static method inside your local class as a workflow:

 int pqr() { class abc { int x; public: abc() : x(4) { } static int pqr() { return abc().x; } }; return abc::pqr(); } 
+3
source

There seems to be a misunderstanding of local classes.

Usually here you can help you as part of a function ... and should NOT go out of scope.

Therefore, it is impossible for the function to take its own local class as an argument, the class is simply not visible from the outside.

Also note that various compilers do not (unfortunately) support these local classes as template parameters (e.g. gcc 3.4), which in fact prevents their use as predicates in STL algorithms.

Usage example:

 int pqr() { class foo { friend int pqr(); int x; foo(): x() {} }; return foo().x; } 

I have to admit that I don't use this much, given the limited scope that I usually use struct instead of class, which means I don't need to worry about friendship;)

+2
source

I don't have a solution for this thing yet (I don’t even know if this can be done), but read this and this to learn more about local classes. This will tell you that you cannot use local classes outside the function in which they are defined (as @ In silico points to answer .)

EDIT This is not possible because this in the article explains:

The name of the function first introduced in a friend's declaration is in the area of ​​the first non-class area containing the enclosing class.

In other words, local classes can only make friends with a function if it was declared as part of their closing function.

+1
source

friend int pqr(abc); in order. This does not work because the type abc not defined before you used it as the type of the parameter in the pqr() function. Define it before the function:

 #include<iostream> // By the way, "using namespace std" can cause ambiguities. // See http://www.parashift.com/c++-faq-lite/coding-standards.html#faq-27.5 using namespace std; // Class defined outside the pqr() function. class abc { int x; public: int xyz() { return x=4; } friend int pqr(abc); }; // At this point, the compiler knows what abc is. int pqr(abc t) { t.xyz(); return tx; } int main() { abc t; cout<<"Return "<<pqr(t)<<endl; } 

I know that you want to use a local class, but what you configured will not work. Local classes are visible only inside the function in which it is defined. If you want to use the abc instance outside the pqr() function, you must define the abc class outside the function.

However, if you know that the abc class will be used only inside the pqr() function, then you can use the local class. But in this case, you need to fix the friend declaration a bit.

 #include<iostream> // By the way, "using namespace std" can cause ambiguities. // See http://www.parashift.com/c++-faq-lite/coding-standards.html#faq-27.5 using namespace std; // pqr() function defined at global scope int pqr() { // This class visible only within the pqr() function, // because it is a local class. class abc { int x; public: int xyz() { return x=4; } // Refer to the pqr() function defined at global scope friend int ::pqr(); // <-- Note :: operator } t; t.xyz(); return tx; } int main() { cout<<"Return "<<pqr()<<endl; } 

This compiles without warning on Visual C ++ (compiler version 15.00.30729.01).

0
source

Source: https://habr.com/ru/post/1315496/


All Articles