简体   繁体   English

来自非成员函数的C ++虚拟继承

[英]C++ Virtual Inheritance from a non-member function

Coming from a Java/C# background and need a bit of help understanding what is happening here in C++... 来自Java / C#背景,需要一些帮助来了解C ++中发生的事情...

class A {
   int x;
   public:
   A(int x) : x(x){}

   void f(int y) {
     cout << x + y << endl;
   }
};

class B : virtual A {
    int x;
    public:
    B(int x) : A(2*x), x(x) {}
    virtual void f(int y){
        cout << x + 2*y << endl;
    }
};

void h(){
    B b(5);
    A &a = dynamic_cast<A &>(b);
    a.f(10);
    b.f
}

void g() {
    A *a = this;
    a->f(10);
    B *b = dynamic_cast<B *>(a);
    b->f(10);
 }

Calling h() is ok but calling g() will not work. 调用h()可以,但是调用g()则无效。 Can someone explain why? 有人可以解释为什么吗? Also, in the line A(int x) : x(x){} what does : x(x){} do? 另外,在A(int x)行中:x(x){}是什么:x(x){}是做什么的? Same question for B(int x) : A(2*x), x(x) and : A(2*x), x(x). B(int x)的相同问题:A(2 * x),x(x)和:A(2 * x),x(x)。

Thanks so much in advance for your help. 在此先感谢您的帮助。

A(int x) : x(x){} what does : x(x){} do? A(int x):x(x){}是做什么的:x(x){}?

: x(x) is the initializer list. : x(x)是初始化列表。 The variable in the paranthesis is the argument received while the outer one is the member variable. 括号中的变量是接收到的参数,而外部变量是成员变量。 It means member variable x is initialized with the value of the x argument received. 这意味着用接收到的x参数的值初始化成员变量x

B(int x) : A(2*x) B(整数x):A(2 * x)

Here you are calling the base class constructor( ie, A ) that receives an integer. 在这里,您正在调用接收整数的基类构造函数(即A )。 x is the variable received by constructor B . x是构造函数B接收的变量。 This is a way of calling parameterized base class constructor from derived class constructor. 这是一种从派生类构造函数调用参数化基类构造函数的方法。 By default, derived class constructor invokes the default base class constructor. 默认情况下,派生类构造函数调用默认的基类构造函数。 In your case, if you don't provide the A(2*x) it fails because the base class has no default constructor. 在您的情况下,如果不提供A(2*x)则会失败,因为基类没有默认构造函数。

g() is just a free function and not a member of a class, so this has no meaning. g()仅仅是一个免费的功能,而不是一个类的成员,所以this是没有意义的。 I'm not exactly sure what you're trying to do there 我不确定您要在那做什么

With regards to: 关于:

A(int x): x(x)

Your A class has int x as a member. 您的A类具有int x作为成员。 This is calling the constructor of that integer with the x value that is passed into the constructor of A . 这是使用传递给A的构造函数的x值调用该整数的构造函数。 In my opinion this is bad style and you should differentiate between the two. 我认为这是不好的风格,您应该区分两者。 For example 例如

class A {
   int x;
   public:
   A(int x_in) : x(x_in){}

   //...
};

This is equivalent to 这相当于

class A {
       int x;
       public:
       A(int x_in)  {
           x = x_in;
       }
       //...
    };

1) As per MSDN (responding to your question related to g() ); 1)根据MSDN (回答有关g() );

The this pointer is a pointer accessible only within the nonstatic member functions of a class, struct, or union type . this指针是仅可在类,结构或联合类型的非静态成员函数中访问的指针。 It points to the object for which the member function is called. 它指向为其调用成员函数的对象。 Static member functions do not have a this pointer. 静态成员函数没有this指针。

2) The A(int y) : x(y) {} initializes A::x (the member before () with the value inside the "()" ie y (modified variable names for better understanding). Same is the case as with B(int x) : A(2*x), x(x) {} . It calls the base class's ( A ) constructor with the parameter 2*x and then initializes B::x with the value inside the () , ie x . 2) A(int y) : x(y) {}初始化A::x()之前的成员,其内部的值是"()"y (修改后的变量名称,以更好地理解)。与B(int x) : A(2*x), x(x) {}它调用基类的( A用参数)构造2*x ,然后初始化B::x与内部的值() ,即x

3) Also, g() wouldn't work because the dynamic_cast<> would throw a compile error since the object that's being casted needs to have at least 1 virtual function . 3)另外, g()无效,因为dynamic_cast<>会引发编译错误,因为要转换的对象至少需要具有1个虚函数 If the only virtual function would be the destructor, then dynamic_cast<> would work. 如果唯一的虚函数是析构函数,则dynamic_cast<>将起作用。

g is a function at file-scope, aka it doesn't belong to any class. g是file-scope的函数,又不属于任何类。 Because of this, you can't use this . 因此,您不能使用this

The : x(x) -style expressions are member constructors - they initialize (ie call the constructor on) the members of the class. : x(x)样式的表达式是成员构造函数-它们初始化(即调用该构造函数)类的成员。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 C ++不支持非成员虚拟函数的原因是什么 - What is the reason non-member virtual functions are not supported in C++ 我可以在C ++中声明一个非成员函数const吗? - Can I declare a non-member function const in C++? C ++:非成员函数的定义/声明的位置 - C++: location of definition/declaration of non-member function C ++ Postfix /前缀运算符重载为非成员函数 - c++ postfix / prefix operator overload as non-member function C++ “在非成员函数中无效使用‘this’”, - C++ “Invalid use of 'this' in non-member function”, How do I call a member function inside a non-member function in C++ with an object created outside the non-member function? - How do I call a member function inside a non-member function in C++ with an object created outside the non-member function? 按位重载和C ++作为非成员函数 - Overloading bitwise & c++ as non-member function C ++ substr方法-“在非成员函数中无效使用&#39;this&#39;” - C++ substr method - “invalid use of ‘this’ in non-member function” 未定义的非成员函数引用 - C ++ - Undefined reference to non-member function - C++ 所有非成员 function 指针在 C++ 中的大小是否相同 - Are all non-member function pointers the same size in C++
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM