考虑:
class A
{
public:
virtual void update() = 0;
}
class B : public A
{
public:
void update() { /* stuff goes in here... */ }
private:
double a, b, c;
}
class C {
// Same kind of thing as B, but with different update function/data members
}
Run Code Online (Sandbox Code Playgroud)
我现在在做:
A * array = new A[1000];
array[0] = new B();
array[1] = new C();
//etc., etc.
Run Code Online (Sandbox Code Playgroud)
如果我调用sizeof(B),返回的大小是3个双成员所需的大小,加上虚函数指针表所需的一些开销.现在,回到我的代码,结果是'sizeof(myclass)'是32; 也就是说,我的数据成员使用24个字节,虚拟功能表使用8个字节(4个虚函数).我的问题是:有什么办法可以简化这个吗?我的程序最终会使用大量的内存,我不喜欢它的25%被虚拟函数指针吃掉的声音.
在下面的示例中,我能够在继承的类中创建一个虚方法,然后在继承类中覆盖它.Show()
我想用受保护的类变量做同样的事情,但我得到错误: prefix
修饰符'virtual'对此项无效
但由于我无法在我的类中将此变量定义为虚拟/覆盖,因此我收到编译器警告:
TestOverride234355.SecondaryTransaction.prefix'隐藏继承的成员'TestOverride234355.Transaction.prefix'.如果要隐藏,请使用new关键字.
幸运的是,当我添加new关键字时,一切正常,这是好的,因为我得到了相同的功能,但这提出了两个问题:
为什么我可以使用虚拟/覆盖方法而不是受保护的类变量?
虚拟/覆盖方法与隐藏它与新方法之间的实际区别是什么,因为至少在这个例子中它们提供相同的功能?
码:
using System;
namespace TestOverride234355
{
public class Program
{
static void Main(string[] args)
{
Transaction st1 = new Transaction { Name = "name1", State = "state1" };
SecondaryTransaction st2 =
new SecondaryTransaction { Name = "name1", State = "state1" };
Console.WriteLine(st1.Show());
Console.WriteLine(st2.Show());
Console.ReadLine();
}
}
public class …Run Code Online (Sandbox Code Playgroud) 我有一个特定的接口查询.默认情况下,接口方法是抽象的和虚拟的,所以如果我们实现该接口并在类中给出定义,我们实际上会覆盖该方法,但是当我们在实现类中将该方法再次标记为虚拟时,为什么编译器不考虑我们实际上在尝试隐藏原始接口虚拟方法.如果我们在基类中有一个虚方法,并且派生类再次将该方法标记为虚拟,那么编译器会发出警告您正在隐藏基类方法,因此如果您故意使用new隐藏基类方法.
public interface ITestInterface
{
void virtualmethod(); // this method is by default virtual.
}
public class TestInterface :ITestInterface
{
public virtual void virtualmethod()
{
// Now compiler should consider that i am actually hiding the interface virtual method.
}
}
Run Code Online (Sandbox Code Playgroud)
如果您为接口构建上述代码并在ILDASM中打开,您将看到如下代码:
.method public hidebysig newslot abstract virtual
instance void virtualmethod() cil managed
{
}//end of method ITestInterface::virtualmethod
# include <iostream>
using namespace std;
class A
{
public:
virtual void f()
{
cout << "A::f()" << endl;
}
};
class B:public A
{
private:
virtual void f()
{
cout << "B::f()" << endl;
}
};
int main()
{
A *ptr = new B;
ptr->f();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
此代码正常工作并打印B :: f().我知道它是如何工作的,但为什么允许这个代码呢?
在顶部,我注意到我的c程序(使用CUDA 3.2)的虚拟大小为28g或更多(查看VIRT),从一开始就进行每次运行.这对我来说没有任何意义.常驻内存是有意义的,在我最大的数据集上只有大约2g.我知道在过去的某个时刻虚拟大小不是那么大,但我不确定何时发生了变化.
为什么我的进程会使用28g的虚拟内存(或者为什么top的VIRT会如此之大)?据我所知,VIRT包括可执行二进制文件(仅437K),共享库和"数据区".什么是"数据区"?如何找出共享库需要多少内存?那个过程总内存的其他元素呢?
/ proc/<pid>/smaps(1022行)的内容:http://pastebin.com/fTJJneXr
其中一个来自smaps的条目显示其中一个占其中的MOST,但没有标签......我怎么能找出这个"空白"条目有28gb?
200000000-900000000 ---p 00000000 00:00 0
Size: 29360128 kB
Rss: 0 kB
Pss: 0 kB
Shared_Clean: 0 kB
Shared_Dirty: 0 kB
Private_Clean: 0 kB
Private_Dirty: 0 kB
Referenced: 0 kB
Anonymous: 0 kB
Swap: 0 kB
KernelPageSize: 4 kB
MMUPageSize: 4 kB
Locked: 0 kB
Run Code Online (Sandbox Code Playgroud)
-
ubuntu 11.04 64位
16 GB RAM
我对虚拟基类的工作原理有点困惑.特别是,我想知道如何调用基类的构造函数.我写了一个例子来理解它:
#include <cstdio>
#include <string>
using std::string;
struct A{
string s;
A() {}
A(string t): s(t) {}
};
struct B: virtual public A{
B(): A("B"){}
};
struct C: virtual public A {};
struct D: public B, public C {};
struct E: public C, public B {};
struct F: public B {};
int main(){
D d;
printf("\"%s\"\n",d.s.c_str());
E e;
printf("\"%s\"\n",e.s.c_str());
F f;
printf("\"%s\"\n",f.s.c_str());
B b;
printf("\"%s\"\n",b.s.c_str());
}
Run Code Online (Sandbox Code Playgroud)
哪个输出
""
""
""
"B"
Run Code Online (Sandbox Code Playgroud)
我不确定前两种情况会发生什么,但对于第三种情况,我至少期待输出为"B".所以现在我只是感到困惑.理解A的构造函数如何被调用的规则是什么?
#include <iostream>
struct B1
{
virtual void method()=0;
virtual ~B1(){}
};
struct B2
{
virtual void method()=0;
virtual ~B2(){}
};
struct D: B1, B2
{
virtual void method()
{
std::cout << "D::method\n";
};
};
int main(int argc,char *argv[])
{
D d;
B1 &b1=d;
B2 &b2=d;
b1.method();
b2.method();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
注意,B1和B2不共享通用接口.
这合法吗?如果是 - 在哪个标准?C++ 98/03/11?
msvc和gcc都编译好了.
以前我想过,我必须使用一些通用接口来处理这种情况(可能的虚拟继承).
这种情况有什么特别的名字吗?
请问它的工作原理如何?也许一些ISO参考?
我试过这段代码:
class A
{
virtual void foo() = 0;
};
class B
{
virtual void foo() = 0;
};
class C : public A, public B
{
//virtual void A::foo(){}
//virtual void B::foo(){}
virtual void A::foo();
virtual void B::foo();
};
void C::A::foo(){}
void C::B::foo(){}
int main()
{
C c;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
使用注释部分时可以,但是当我尝试在类声明之外编写定义时,编译器会报告错误.我正在使用MSVC11编译器,有谁知道怎么写这个?我需要将代码移动到cpp文件中.
谢谢~~
我带着一段我不理解的代码的类声明:
class Weapon
{
public:
virtual void attack() const = 0;
};
Run Code Online (Sandbox Code Playgroud)
这const = 0部分是什么意思?
我有一个MAC和OPT-ò(意大利语键盘布局)我可以在所有应用程序中编写"@"但不能在"Android模拟器"中编写.我不想在我的应用程序中通过虚拟键盘按"char per char".没有虚拟键盘可以写"@"吗?
virtual ×10
c++ ×6
c# ×2
memory ×2
overriding ×2
android ×1
base-class ×1
c ×1
class ×1
cuda ×1
function ×1
inheritance ×1
interface ×1
keyboard ×1
linux ×1
methods ×1
new-operator ×1
oop ×1