在followint代码中,指针转换和多继承如何一起发挥作用?
class Foo {
public:
virtual void someFunc();
};
class Bar;
void someWork(Bar *bar) {
((Foo*) bar)->someFunc();
}
class Bar: public Zed, public Foo {
...
virtual void someFunc() { ... do something else ... }
}
Bar bar;
int main() {
someWork(&bar);
}
Run Code Online (Sandbox Code Playgroud)
我的理解有点不稳定.
一方面,有些工作对Bar一无所知,所以这不应该工作; 但另一方面,我已经向前宣布了Bar.
谢谢!
c++ virtual-functions multiple-inheritance forward-declaration
在objective-c中,可以向属性添加@dynamic.
这也适用于普通的实例方法吗?
编辑 我认为我不够清楚.
我想做以下事情:
@interface MyClass
@property (retain) NSObject *somePropertyObject;
- (void) myMethod;
@end
@implementation MyClass
@dynamic somePropertyObject;
//Make myMethod dynamic. I do not want to implement it. Like C++ Virtual
@end
Run Code Online (Sandbox Code Playgroud) 使用C++,我有
struct Base {
virtual void stuff(/*base stuff*/);
};
struct Derived : public Base {
void stuff(/*derived stuff*/);
};
void function1(Derived& obj){
obj.stuff();
}
Run Code Online (Sandbox Code Playgroud)
在这种情况下,function1将使用Derived的do()函数.如果在function1中,我想调用Base类的do()函数呢?如果我将function1称为function1(dynamic_cast<Base*>(derived_obj_ptr))?它会起作用
吗?
在Java,C#和D等语言中,final或者sealed保证类是叶类(没有其他类继承的类).这允许编译器对编译时类型为final或的对象进行顶级devirtualize方法调用sealed.
C++没有final或sealed关键字.有没有办法模拟其在我的继承层次结构中作为叶子的类的去虚拟化的行为?
我知道你不应该在基类的ctor或dtor中调用任何虚函数,但是从派生类最多的那个呢?应该没事吧?例如
class base {
...
virtual void free() = 0;
};
class child : public base {
...
free() {/* free memory */}
~child() {free();}
};
Run Code Online (Sandbox Code Playgroud) C++大师.
我试图在C++中实现多态.我想编写一个带有虚函数的基类,然后在子类中重新定义该函数.然后在我的驱动程序中演示动态绑定.但我无法让它发挥作用.
我知道如何在C#中实现它,所以我想我可能在我的C++代码中使用C#的语法时犯了一些语法错误,但这些错误对我来说并不明显.如果你能纠正我的错误,我将非常感激.
#ifndef POLYTEST_H
#define POLYTEST_H
class polyTest
{
public:
polyTest();
virtual void type();
virtual ~polyTest();
};
#endif
#include "polyTest.h"
#include <iostream>
using namespace std;
void polyTest::type()
{
cout << "first gen";
}
#ifndef POLYCHILD_H
#define POLYCHILD_H
#include "polyTest.h"
using namespace std;
class polyChild: public polyTest
{
public:
void type();
};
#endif
#include "polyChild.h"
#include <iostream>
void polyChild::type()
{
cout << "second gen";
}
#include <iostream>
#include "polyChild.h"
#include "polyTest.h"
int main()
{
polyTest * ptr1;
polyTest * ptr2; …Run Code Online (Sandbox Code Playgroud) 我可以理解基本的对象切片,但有时当我们来到这样的例子时它会变得很混乱
using System;
class A
{
public virtual void F() { Console.WriteLine("A.F"); }
}
class B: A
{
public override void F() { Console.WriteLine("B.F"); }
}
class C: B
{
new public virtual void F() { Console.WriteLine("C.F"); }
}
class D: C
{
public override void F() { Console.WriteLine("D.F"); }
}
class Test
{
static void Main() {
D d = new D();
A a = d;
B b = d;
C c = d;
a.F();
b.F();
c.F();
d.F();
} …Run Code Online (Sandbox Code Playgroud) 我有以下代码.
class A {
public:
virtual void foo() = 0;
}
class B : public A {
public:
void bar() { /* Do something */ }
}
void B::A::foo() {
bar();
// Do something else
}
Run Code Online (Sandbox Code Playgroud)
当我尝试编译这个...我得到一个错误,说它无法找到bar().这不是实例化纯虚函数的正确方法吗?
use of undeclared identifier 'bar'
Run Code Online (Sandbox Code Playgroud) 我试图理解虚函数和虚继承.在大多数情况下,我认为我成功地掌握了它及其与多态的关系,我一直在阅读vptr如何与派生对象一起工作以及什么不是,但是下面的例子让我失望,这是我在算法和C++中的数据结构书:
#include <iostream>
using namespace std;
class Class1 {
public:
virtual void f() {
cout << "Function f() in Class1\n";
}
void g() {
cout << "Function g() in Class1\n";
}
};
class Class2 {
public:
virtual void f() {
cout << "Function f() in Class2\n";
}
void g() {
cout << "Function g() in Class2\n";
}
};
class Class3 {
public:
virtual void h() {
cout << "Function h() in Class3\n";
}
};
int main() {
Class1 object1, *p; …Run Code Online (Sandbox Code Playgroud) 我在使用自动存储持续时间声明的对象使用虚函数时遇到问题.这是一个可重现的场景:
#include <iostream>
class A {
public:
A() {}
virtual ~A() {}
virtual void printClassName() {
std::cout << "A" << std::endl;
}
};
class B : public A {
public:
B() : A() {}
~B() {}
void printClassName() {
std::cout << "B" << std::endl;
}
};
class Test {
private:
A item;
public:
Test() {}
~Test() {}
void setItem(A item) {
this->item = item;
}
A getItem() {
return this->item;
}
};
int main() {
Test t;
B item;
t.setItem(item); …Run Code Online (Sandbox Code Playgroud) c++ ×8
inheritance ×3
polymorphism ×2
c# ×1
c++11 ×1
clang++ ×1
dynamic ×1
objective-c ×1
oop ×1
performance ×1
properties ×1