考虑:
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) 在顶部,我注意到我的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
我有一个MAC和OPT-ò(意大利语键盘布局)我可以在所有应用程序中编写"@"但不能在"Android模拟器"中编写.我不想在我的应用程序中通过虚拟键盘按"char per char".没有虚拟键盘可以写"@"吗?
我想要一个纯虚拟父类来调用函数的子实现,如下所示:
class parent
{
public:
void Read() { //read stuff }
virtual void Process() = 0;
parent()
{
Read();
Process();
}
}
class child : public parent
{
public:
virtual void Process() { //process stuff }
child() : parent() { }
}
int main()
{
child c;
}
Run Code Online (Sandbox Code Playgroud)
这应该工作,但我得到一个未链接的错误:/这是使用VC++ 2k3
或者不应该工作,我错了吗?
我有一个在客户端应用程序和服务器应用程序中使用的类.在服务器应用程序中,我通过扩展方法向类添加了一些功能.效果很好.现在我想要更多:
我的班级(B)继承自另一个班级(A).
我想将一个虚函数附加到A(比如说Execute()),然后在B中实现该函数.但只在服务器中实现.Execute()方法需要使用只有服务器知道的类型才能完成只能在服务器上执行的操作.
有很多类型的继承自A,就像B一样,我想为它们中的每一个实现Execute().
我希望我可以为A添加一个虚拟扩展方法,但这个想法似乎没有飞过.无论有没有扩展方法,我都在寻找解决这个问题的最优雅的方法.
我有枚举的问题
我需要在基类或接口中创建一个枚举(但是空的)
class Base
{
public enum Test;
// ???
}
Run Code Online (Sandbox Code Playgroud)
并在一些父类中创建不同的枚举之后
class Parent1
{
public enum Test {A, B, C};
}
class Parent2
{
public enum Test {J, H, K};
}
Run Code Online (Sandbox Code Playgroud)
现在,当我必须使用枚举时,我有下一节课的方法
class Test<T>
{
public void Foo(Test enum)
{
int value = (int) enum;
// ...
}
}
Run Code Online (Sandbox Code Playgroud)
有没有办法做那样的事情?
如果不是,我必须在每个班级使用静态内联...
class Parent1
{
public static int A = 0;
public static int B = 5;
public static int C = 7;
}
class Parent2
{
public static int …Run Code Online (Sandbox Code Playgroud) 我的代码中有类似的东西:
#include <iostream>
#include <cstdlib>
struct Base
{
virtual int Virtual() = 0;
};
struct Child
{
struct : public Base
{
virtual int Virtual() { return 1; }
} First;
struct : public Base
{
virtual int Virtual() { return 2; }
} Second;
};
int main()
{
Child child;
printf("ble: %i\n", ((Base*)&child.First)->Virtual());
printf("ble: %i\n", ((Base*)&child.Second)->Virtual());
system("PAUSE");
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我希望这会给出这个输出:
ble: 1
ble: 2
Run Code Online (Sandbox Code Playgroud)
当它在GCC下编译时(3.4.5我相信).
但是,在Visual Studio 2008下编译并运行它,可以得到:
ble: 2
ble: 2
Run Code Online (Sandbox Code Playgroud)
有趣的是,如果我给出Base-derived structs names(struct s1 : …
我在单元测试中有以下代码
public bool TestMethodsOf<T, I>()
{
var impl = typeof(T);
var valid = true;
foreach (var iface in impl.GetInterfaces().Where(i => typeof(I).IsAssignableFrom(i)))
{
var members = iface.GetMethods();
foreach (var member in members)
{
Trace.Write("Checking if method " + iface.Name + "." + member.Name + " is virtual...");
var implMember = impl.GetMethod(member.Name, member.GetParameters().Select(c => c.ParameterType).ToArray());
if (!implMember.IsVirtual)
{
Trace.WriteLine(string.Format("FAILED"));
valid = false;
continue;
}
Trace.WriteLine(string.Format("OK"));
}
}
return valid;
}
Run Code Online (Sandbox Code Playgroud)
我打电话给他
Assert.IsTrue(TestMethodsOf<MyView, IMyView>());
Run Code Online (Sandbox Code Playgroud)
我想确保界面中的所有方法都声明为虚拟.原因是因为我正在应用spring.net方面,它只适用于虚方法.
我遇到的问题是implMember.IsVirtual总是正确的,即使它们在声明类型中没有声明它们也是如此.
我的TestMethodsOf逻辑有什么问题?
干杯
在Eclipse中,默认情况下禁用虚拟空间(在行结束后放置插入符的可能性),这与IntelliJ IDEA默认设置相反.http://www.jetbrains.com/idea/documentation/migration_faq.html
是否有一个特殊原因可以让您在行结束后放置插入符号?我试图想一个用例,但不能提出任何......
我问的原因是因为它是IntelliJ中的默认设置让我很好奇.
virtual ×10
c# ×4
c++ ×3
memory ×2
android ×1
c ×1
class ×1
cuda ×1
enums ×1
inheritance ×1
keyboard ×1
linux ×1
new-operator ×1
oop ×1
overriding ×1
parent ×1
reflection ×1
space ×1
spring.net ×1