C ++ implicitly calls the When function? And How?

I have a couple of questions.

All functions inside class member functions? or only those preceded by the declaration "friend"?

The meaning of member functions is that they cannot be accessed by any other classes correctly?

What is the difference between implicit and explicit call?

What functions may or may not be implicitly called?

I was hoping to see an example of an implicit and explicit call.

EDIT: Thanks for the great answers, there were a lot of bits and parts that answered my question and thanks for the links to the books. I will read them.

+3
source share
9 answers

Are all functions inside a class member functions? or only those preceded by the declaration "friend"?

Friend -. , , , . :

class myclass
{
    friend void fun(const myclass& obj);
    int x;
};

void fun(const myclass& obj)
{
    std::cout << obj.x; // x is private member
}

?

() operator, . , . :

fun();

:

void someScope(){
    myclass myobject; // constructors called

} // destructor of myobject is called before exiting the function
....
myclass* mySecondObject = new myclass; // constructor called
delete mySecondObject; // destructor called
+2

++ . , , .

+7

, operator type() .

+4

, : ++.

, : , , . "friend" , , .

, , .

++ , .

+3

.

struct A { };
void operator + (A, A);

struct B {
  void operator + (B);
  void f ();
};

A a;
void B::f() {
  operator+ (a,a); // ERROR – global operator hidden by member
  a + a; // OK – calls global operator+
}

, , "": , , .

-: - - , , , - , - .

. , operator+(10, 12);. , , .


? , ""?

- , , ,

struct A {
    friend void f() {
      std::cout << "member of the global namespace" << std::endl;
    }

    void g() {
      std::cout << "member of class A" << std::endl;
    }
};

f A - , () A, A::.


-, , . .

+3

? , "". () . , "", - ( , ). non-friend -.

- "" .

. :

class BigBuffer
{
public:
 BigBuffer(int initialValue)
   { memset(buffer, initialValue, sizeof(buffer)); }
private:
 char buffer[65536];
};

extern void Foo(const BigBuffer& o);

void oops()
{
 Foo(3);
}

Foo (3) BigBuffer 3 BigBuffer, Foo.

0

:

-, , "". , , .

- - , - . private, private:, , . protected, , , public, , . , friend, private protected .

, . explicit, .

Foo ,

Foo(int i);
explicit Foo(const char * s);

, int -, , Foo, , , ,

f(Foo f);

-,

f(1);
f(Foo("foo"));

,

f("foo");

.

, ++. StackOverflow ++-, ++ FAQ Lite. , , , -.

0

, , .

, .

:

class FooA
{
public:
   FooA();
};

class FooB
{
public:
   FooB();
   explicit FooB(const FooA &other);
};

, :

void MyFunc(FooB &var) { ... }

void main()
{
   FooA bar;
   MyFunc(bar);

   FooB foo(bar);
}

FooA FooB- , "" . , , FooA fooB, MyFunc().

, , foo bar, .

0

, ...

. . :

class A
{
   void hidden();
   friend class B;
};

class B
{
   void callIntoA( A& a )
   {
       a.hidden();
   }
};

, - , "public:" :

class A
{
    void hidden();

public:
    void notHidden();
    void alsoNotHidden();

private:
    void alsoHidden();
};
-1

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


All Articles