Function declared outside class scope but not friend. How does this work?

Abrar

I have the following class:

class PhoneCall{
private:
    string number;
public:
    /*some code here */
};

Now, I have declared a function (not friend to PhoneCall) which does some specific operation and returns a PhoneCall object

PhoneCall callOperation()

Another which takes a PhoneCall object as parameter

void userCall(PhoneCall obj)

I was expecting it not to work unless it is explicity declared as a friend to that class.

Why and how do these functions work even when they are not friend to the PhoneCall class ?

A friend function of a class is defined outside that class' scope but it has the right to access all private and protected members of the class http://www.tutorialspoint.com/cplusplus/cpp_friend_functions.htm

oo_miguel

You can pass, manipulate and return instances of a class without beeing its friend, as long as you do not access its private or protected members.

According to N4296. p. 261:

11.3 Friends

A friend of a class is a function or class that is given permission to use the private and protected member names from the class.

Unless you declare your move or copy constructors as private or protected, the object can be also copied or moved as a whole.

So practically a private PhoneCall constructor will prevent non-friends from instantiating PhoneCall objects:

For example:

class PhoneCall{
    private: PhoneCall(){}
};

This prevents non-friend code from instantiating the class:

PhoneCall callOperation(){
    return PhoneCall();
}

will results in a compile-time error:

error: 'PhoneCall::PhoneCall()' is private

Edit: Added info on private constructors following M.M`s suggestion in the comments.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Friend function inside class and outside class, what difference does it make?

From Dev

Which scope does an in-class-defined friend function belong to?

From Dev

how to overload operator == outside template class using friend function?

From Dev

how to overload operator == outside template class using friend function?

From Dev

Not declared in scope - friend comparator class for priority_queue C++

From Dev

How do I get an ng-show to work inside an ng-repeat on a variable declared outside that scope?

From Dev

How do I get an ng-show to work inside an ng-repeat on a variable declared outside that scope?

From Dev

In C, if objects declared at block scope have no linkage, why does function declaration inside main() without "extern" work?

From Dev

Conditional statements in a class, but outside of scope of the function

From Dev

Variables declared outside the class

From Dev

Why must a function, declared as friend in Class A, be defined as a public function in Class B?

From Dev

Why does this Rails code work if there are methods declared outside the Contoller?

From Dev

Global variable declared outside function doesn't work

From Dev

How does `<$` function work in the Functor class?

From Dev

Codeblocks: 'Class' was not declared in this scope

From Dev

Class objects not declared in scope

From Dev

Passing scope vars to a closure that was declared outside of that scope

From Dev

friend not allowed outside of a class definition

From Dev

why does swift dictionary with function work outside of a class but produces error inside of a class?

From Dev

When does a variable need to be declared outside a function in Javascript?

From Dev

Typescript does not load array declared outside a function. Please suggest

From Dev

Function pointer "was not declared in this scope"

From Dev

Are friend functions inherited? and why would a base class FRIEND function work on a derived class object?

From Dev

Function 'not declared in this scope' when using class in C++

From Dev

NLog does not work outside MainForm class

From Dev

Friend function scope and point of declaration

From Dev

In functional programming, can a function call another function that was declared outside of it's scope and not passed as a parameter?

From Dev

How does scope work in Io?

From Dev

How does scope work in Ruby?

Related Related

  1. 1

    Friend function inside class and outside class, what difference does it make?

  2. 2

    Which scope does an in-class-defined friend function belong to?

  3. 3

    how to overload operator == outside template class using friend function?

  4. 4

    how to overload operator == outside template class using friend function?

  5. 5

    Not declared in scope - friend comparator class for priority_queue C++

  6. 6

    How do I get an ng-show to work inside an ng-repeat on a variable declared outside that scope?

  7. 7

    How do I get an ng-show to work inside an ng-repeat on a variable declared outside that scope?

  8. 8

    In C, if objects declared at block scope have no linkage, why does function declaration inside main() without "extern" work?

  9. 9

    Conditional statements in a class, but outside of scope of the function

  10. 10

    Variables declared outside the class

  11. 11

    Why must a function, declared as friend in Class A, be defined as a public function in Class B?

  12. 12

    Why does this Rails code work if there are methods declared outside the Contoller?

  13. 13

    Global variable declared outside function doesn't work

  14. 14

    How does `<$` function work in the Functor class?

  15. 15

    Codeblocks: 'Class' was not declared in this scope

  16. 16

    Class objects not declared in scope

  17. 17

    Passing scope vars to a closure that was declared outside of that scope

  18. 18

    friend not allowed outside of a class definition

  19. 19

    why does swift dictionary with function work outside of a class but produces error inside of a class?

  20. 20

    When does a variable need to be declared outside a function in Javascript?

  21. 21

    Typescript does not load array declared outside a function. Please suggest

  22. 22

    Function pointer "was not declared in this scope"

  23. 23

    Are friend functions inherited? and why would a base class FRIEND function work on a derived class object?

  24. 24

    Function 'not declared in this scope' when using class in C++

  25. 25

    NLog does not work outside MainForm class

  26. 26

    Friend function scope and point of declaration

  27. 27

    In functional programming, can a function call another function that was declared outside of it's scope and not passed as a parameter?

  28. 28

    How does scope work in Io?

  29. 29

    How does scope work in Ruby?

HotTag

Archive