小编tes*_*ram的帖子

私有函数成员在类之外调用

在下面的例子中,为什么B::f()即使它是私有的呢?

我知道这一事实:使用表达式的类型在调用点检查访问,该表达式用于表示调用成员函数的对象.

#include <iostream>

class A {
public:
  virtual void f() { std::cout << "virtual_function"; }
};

class B : public A {
private:
  void f() { std::cout << "private_function"; }
};

void C(A &g) { g.f(); }

int main() {
  B b;
  C(b);
}
Run Code Online (Sandbox Code Playgroud)

c++ c++11

15
推荐指数
2
解决办法
774
查看次数

结构和数组('Array'的初始化程序太多)C++

为什么我会收到错误:'Array'的初始化程序太多它的C++ 11代码我不知道问题出在哪里

#include <iostream>
using namespace std;

struct Point {
int x,y ;
};
Point points[3] {{1,2},{3,4},{5,6}};
int x2 = points[2].x;

struct Array {
Point elem[3];
};

int main() {
    cout << "!!!\nStructure!!!" << endl; //

    Array points2  {{1,2},{3,4},{5,6}};// *too many initializers for‘Array’* 
    int y2 = points2.elem[2].y;

    cout << "!!!here points2 = !!!" << y2 <<endl;

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

c++ arrays structure c++11

2
推荐指数
1
解决办法
551
查看次数

最令人烦恼的解析C++ 11

Y y {X{}};对这条线究竟是做什么以及它与最令人烦恼的解析的联系感到困惑.简要说明表示赞赏:

#include <iostream>

struct X {
  X() { std::cout << "X"; }
};
struct Y {
  Y(const X &x) { std::cout << "Y"; }
  void f() { std::cout << "f"; }
};
int main() {
  Y y {X{}};
  y.f();
}
Run Code Online (Sandbox Code Playgroud)

c++ c++11

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

c ++ using-declarations用于从基类调用函数

为什么要d.f(1)调用Derived::f此代码?

是否using Base::f决定其发挥作用f将被调用?

#include <iostream>
using namespace std;

struct Base {
    void f(int){
        cout << "\n f(Base) is called" ;
    }
};
struct Derived : Base {
    using Base::f ;    //  using-declarations  but still Drived function is called
    void f(int){
        cout << "\n f(Derived) is called" ;
    }
};
void use(Derived d)
{

    d.f(1);     // calls Derived::f
    Base& br = d ;
    br.f(1);        // calls Base::f
}

int main() {

        Derived d; …
Run Code Online (Sandbox Code Playgroud)

c++

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

标签 统计

c++ ×4

c++11 ×3

arrays ×1

structure ×1