标签: friend

C++类,友元运算符与外部运算符有什么区别

当我们在类中定义运算符函数时,我们也在类中定义了它,那么该函数就不是类的一部分。

但当该函数位于类外部并且我们将其声明为类内部的友元但不定义它时,也会实现相同的任务。

考虑一下这段代码,它有两个相同的运算符定义,其中一个在类内部,另一个在类外部:

版本 1(在类内部)

class MyClass
{
    // version 1 inside a class
    friend MyClass&& operator +(MyClass& a, MyClass& b)
    {
        return move(MyClass(a.x + b.x, a.y + b.y));
    }
    int x,y;

public:
    MyClass() {}
    MyClass(int,int){}
};

int main()
{
    MyClass a, b, c;
    c = a + b;
    cin.ignore();
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

版本 2(在类之外)

class MyClass
{
      friend MyClass&& operator +(MyClass& a, MyClass& b);
    int x,y;

public:
    MyClass() {}
    MyClass(int,int){}
};

MyClass&& operator +(MyClass& a, MyClass& b)
{
    return …
Run Code Online (Sandbox Code Playgroud)

c++ class friend operator-keyword

0
推荐指数
1
解决办法
1264
查看次数

朋友类:无法访问私人成员?

我只是在学习 C++ 朋友课程。正如书上所说,朋友类也可以访问所有成员(私有和受保护的)。但就我而言,情况并非如此。

我知道有一个我看不到的愚蠢错误。请帮我找到它:D

我的代码:

#include <iostream>
using namespace std;

class A;
class B {
private:
    int num;
public:
    B(int n=0):num(n){}

    friend int add(A, B);
    friend int mul(A, B);
    friend int sub(A, B);
    void showthis(A);
    friend class A;
};

class A{
private:
    int num;
public:
    A(int n=0):num(n){}

    friend int add(A, B);
    friend int mul(A, B);
    friend int sub(A, B);   
};

int add(A a, B b){
    return a.num+b.num;
}

int sub(A a, B b){
    return a.num-b.num;
}

int mul(A a, B …
Run Code Online (Sandbox Code Playgroud)

c++ friend

0
推荐指数
1
解决办法
5863
查看次数

在全局命名空间中声明一个返回模板类的友元函数

我在命名空间中声明了一个函数,当它被定义为友元时,会引发编译器错误。

#include <string>
#include <vector>

namespace Example
{
    std::vector<std::string> GetNames();
}

class MyClass
{
    public:
        MyClass() : 
                name("data")
        {}

    private:
        std::string name;

    friend std::vector<std::string> ::Example::GetNames();
};
Run Code Online (Sandbox Code Playgroud)

这无法编译错误

'class std::vectorstd::__cxx11::basic_string<char >' 中的 'Example' 没有命名类型朋友 std::vectorstd::string ::Example::GetNames(); ^~~~~~~ main.cpp:25:2: 错误:应为';' 类定义后

然而,如果我删除全局命名空间标识符::from ::Example::GetNames(),它将愉快地编译。

当由于嵌套命名空间而需要从全局命名空间开始时,我该如何处理这个问题?

c++ class friend

0
推荐指数
1
解决办法
65
查看次数

如何将全局范围函数声明为命名空间类的朋友?

以下代码class Foonamespace Namespace.

// main.cpp
#include <csignal>

namespace Namespace
{

class Foo
{
private:
  void doSomething() {};

friend void func( union sigval );
};

}

static Namespace::Foo foo;

void func( union sigval sv ) {
  (void)sv;
  foo.doSomething();
}
Run Code Online (Sandbox Code Playgroud)

我想和void func( union sigval )这个类交个朋友,这样它就可以调用一个私有函数。执行此操作的正确语法是什么?以上失败并出现以下错误

$ g++ --version && g++ -g ./main.cpp
g++ (Debian 6.3.0-18+deb9u1) 6.3.0 20170516
Copyright (C) 2016 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. …
Run Code Online (Sandbox Code Playgroud)

c++ namespaces friend friend-function

0
推荐指数
1
解决办法
42
查看次数

Can a virtual function be a friend of another class?

Everywhere in theory, it is written that "A virtual function can be declared as a friend of another class", but practically on implementing it, the compiler throws an error saying "virtual functions cannot be friends".

Here is a C++ program to illustrate this:

#include <iostream>
using namespace std;

class extra;
class base {
public:
    virtual friend  void print(extra e);

    void show()
    {
        cout << "show base class" << endl;
    }
};

class derived : public base {
public:
    void …
Run Code Online (Sandbox Code Playgroud)

c++ virtual friend

0
推荐指数
1
解决办法
364
查看次数

C++ 编译器不考虑友元类声明

我正在尝试重写教科书(C++ Primer)中显示的程序。这是一个类似电子邮件的小程序,包含两个 mainMessageFolder类。源文件如下:

\n

文件夹.h

\n
#pragma once\n\n#include <set>\n#include <string>\n\n#include "Message.h"\n\nusing std::string;\nusing std::set;\n\n\nclass Folder {\n    friend class Message;\n    friend void swap(Folder&, Folder&);\n\npublic:\n    Folder(const string &fname):\n        name(fname) { }\n    Folder(const Folder&);\n    Folder& operator=(const Folder&);\n    ~Folder();\n\n    void print_debug();\n\nprivate:\n    string name;\n    set<Message*> msgs;\n\n    void add_to_Message(const Folder&);\n    void remove_from_Message();\n\n    void addMsg(Message*);\n    void remMsg(Message*);\n};\n
Run Code Online (Sandbox Code Playgroud)\n

消息.h

\n
#pragma once\n\n#include <string>\n#include <set>\n\nusing std::set;\nusing std::string;\n\nclass Folder;\n\nclass Message {\n    friend class Folder;\n    friend void swap(Message&, Message&);\n\npublic:\n    // folders is implicitly initialized to the empty …
Run Code Online (Sandbox Code Playgroud)

c++ friend private-members

0
推荐指数
1
解决办法
146
查看次数

为什么“operator&lt;&lt;()”重载定义中需要“friend”?

class Train{
public: 
    char direction;
    int loading_time, crossing_time;        
    ...

    friend std::ostream& operator<<(std::ostream& os, const Train& t){
        os << t.direction << '/' << t.loading_time << '/' << t.crossing_time;
        return os;
    }
};
Run Code Online (Sandbox Code Playgroud)

为什么在这种情况下需要“朋友”?所有属性都是公共的。我应该只使用结构体吗?

c++ class operator-overloading friend

0
推荐指数
1
解决办法
939
查看次数

`inline friend ......`函数和`friend inline .....'函数有什么区别?

当我从string.h文件中检查声明时,我看到了以下内容:

friend inline void     cat(const String&,const SubString&,const String&,String&);
inline friend void     cat(const String&,const SubString&,const String&,String&);
Run Code Online (Sandbox Code Playgroud)

他们之间有什么区别?

c++ inline friend

-1
推荐指数
1
解决办法
3443
查看次数

C++:朋友作为课堂上的主要人物

主要功能可以在C++中成为好友功能吗?

 #include "stdafx.h"
#include <iostream>
using namespace std;
class A {
public:
    A():i(10){}
private:
    int i;
    friend int main();
};

int main()
{
    A obj;
    cout<<obj.i;
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

c++ constructor class friend

-3
推荐指数
1
解决办法
264
查看次数