我有一个具有大量虚方法的ac #Class,其中一些方法基本上是抽象的(它们完全在子类中实现,基类是空的).
为了让它进行编译,我在基类中抛出一个InvalidOperationException,并对应该做什么做出评论.这只是感觉很脏.
有没有更好的方法来设计我的课程?
编辑:对于将在加拿大运行的应用程序的中间层,一半的方法是通用的,因此是虚拟的.一半的方法都是省特定的.
Public class PersonComponent()
{
public GetPersonById(Guid id) {
//Code to get person - same for all provinces
}
Public virtual DeletePerson(Guid id) {
//Common code
}
Public virtual UpdatePerson(Person p) {
throw new InvalidOperation("I wanna be abstract");
}
Public Class ABPersonComponent : PersonComponent
{
public override DeletePerson(Guid id)
{
//alberta specific delete code
}
public override UpdatePerson(Person p)
{
//alberta specific update codecode
}
}
Run Code Online (Sandbox Code Playgroud)
希望这是有道理的
#include<iostream>
class base{
public:
base(){std::cout<<"In base";}
};
class dv1:virtual private base {
public:
dv1(){std::cout<<"In DV1";}
};
class dv2:virtual private base {
public:
dv2(){std::cout<<"In DV2";}
};
class drv : public dv1, public dv2 {
public:
drv() {std::cout<<"Why is this working";}
};
int main() {
drv obj;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
不是在虚拟继承的情况下,调用构造函数是大多数派生类的责任吗?注意:这里基地是虚拟和私下继承的.
class a //my base class
{
public:
a()
{
foo();
}
virtual void foo() = 0;
};
class b : public a
{
public:
void foo()
{
}
};
int main()
{
b obj; //ERROR: undefined reference to a::foo()
}
Run Code Online (Sandbox Code Playgroud)
为什么它会给我错误?纯虚拟foo已定义.我需要在代码中进行哪些更改才能使其正常工作?我需要在其构造函数中调用基类的纯虚方法.
有人可以向我解释为什么在将类转换为基类时调用重写方法:
class Base
{
public virtual void VirtualMethod()
{
Console.WriteLine("Base virtual method");
}
}
sealed class Derived : Base
{
public override void VirtualMethod()
{
Console.WriteLine("Overriden method");
}
}
static void Main(String[] args)
{
Derived d = new Derived();
((Base)d).VirtualMethod();
}
Run Code Online (Sandbox Code Playgroud)
我的意思是这段代码打印:
Overriden method
Run Code Online (Sandbox Code Playgroud)
并不是
Base virtual method
Run Code Online (Sandbox Code Playgroud)
它是一个运行时或编译时的未来?
我知道我可以通过调用派生来调用Base的虚方法,base.VirtualMethod()但是我可以从外面调用它吗?(比如来自Main或其他一些班级)
在我的程序中,我有这样的类层次结构:
#include <iostream>
#include <cmath>
#include <sstream>
using namespace std;
class aa;
class bb;
class root
{
public:
virtual ~root() {}
virtual root add(const aa& a) const=0;
virtual root add(const bb& a) const=0;
};
class aa: public root
{
public:
aa() { }
aa(const aa& a) { }
virtual root add(const aa& a) const
{ return root(new aa()); }
virtual root add(const bb& a) const
{ return root(new bb()); }
};
class bb: public root
{
public:
bb() { } …Run Code Online (Sandbox Code Playgroud) 我对以下代码的编译器错误感到困惑:
class Base {
public:
virtual ~Base () { }
virtual void func () { }
virtual void func (int) { }
virtual void another () { }
virtual void another (int) { }
};
class Derived : public Base {
public:
void func () { }
};
int main () {
Derived d;
d.func();
d.func(0);
d.another();
d.another(0);
}
Run Code Online (Sandbox Code Playgroud)
使用gcc 4.6.3,上面的代码在d.func(0)处导致错误,说Dervied :: func(int)是未定义的.
当我还为Derived添加func(int)的定义时,它就可以了.当我在Derived中既未定义func()也未定义func(int)时,它也有效(如"另一个"的情况).
显然这里有一些关于虚拟重载函数的规则,但这是我第一次遇到它,我不太明白它.有人能告诉我这到底发生了什么吗?
class A
{
public:
void virtual magic() = 0;
void bar()
{
magic(); // this should be legal
}
};
class B: public A
{
public:
void magic()
{
cout<<"implement magic here"<<endl;
}
};
class C: public B
{
void foo()
{
magic(); // this should not be allowed, i.e. create compile-time error
}
};
Run Code Online (Sandbox Code Playgroud)
因此,该纯虚基类A的B应访问magic(),但不排除任何派生类C的B.这可以使用访问说明符和/或朋友声明或以任何其他方式实现吗?
我正在尝试使用Proftpd配置SFTP.我已成功配置SFTP与Unix用户,但现在我想使用虚拟用户,因为我将有大量用户添加/删除.我希望用户使用用户名/密码登录.
为此,我首先按照教程使用SFTP和ProFTPd然后尝试其他人.
以下是SFTP服务器的配置:
Include /etc/proftpd/modules.conf
ServerName "Nom du FTP"
ServerType standalone
DefaultServer on
# Umask 022 is a good standard umask to prevent new dirs and files
# from being group and world writable.
Umask 022
# To prevent DoS attacks, set the maximum number of child processes
# to 30. If you need to allow more than 30 concurrent connections
# at once, simply increase this value. Note that this ONLY works
# in standalone mode, in …Run Code Online (Sandbox Code Playgroud) 我遇到了课堂模板std::unary_function和std::binary_function.
template <class Arg, class Result>
struct unary_function {
typedef Arg argument_type;
typedef Result result_type;
};
template <class Arg1, class Arg2, class Result>
struct binary_function {
typedef Arg1 first_argument_type;
typedef Arg2 second_argument_type;
typedef Result result_type;
};
Run Code Online (Sandbox Code Playgroud)
这两者都可以用作特定目的的基类.但仍然没有虚拟析构函数.我可以猜到的一个原因是这些并不意味着要进行多态处理.即
std::unary_function* ptr;
//intialize it
//do something
delete ptr;
Run Code Online (Sandbox Code Playgroud)
但如果是这样的话,不应该在析构函数中使用protected访问说明符,这样编译器就会破坏任何尝试.
我正在学习多重继承和钻石问题,当我从最派生的类中调用函数时,Visual Studio告诉我调用是不明确的:
struct A
{
virtual void aFunction() { cout << "I am A\n"; }
};
struct B : A {};
struct C : A {};
struct D : B, C {};
int main()
{
D DObj;
DObj.aFunction(); // This is an ambiguous call
}
Run Code Online (Sandbox Code Playgroud)
我理解如果我在B和C类中重写了基类函数,那么调用将是不明确的,但在B和C中不是"aFunction()"相同吗?
此外,使B和C从A继承实际上会使错误消失.但是我在继承时对关键字"虚拟"的理解,即(Derived:virtual Base),是因为它阻止了链中更多的"派生类"继承链上的多个副本.在继承中,可以继承成员变量的多个副本,但只能复制具有相同名称的函数的一个副本.因此,例如,我可以有5个派生类,每个派生自Base,然后是从所有5个派生类继承的MostDerivedClass,在MostDerivedClass中,我将有5个副本的基类"成员变量",但只有一个函数的副本同名.
换句话说,继承的"虚拟"关键字应该防止多个Base"成员变量"副本.我不明白为什么在这种情况下它会清除一个模糊的函数调用.
编辑:谢谢,它正在慢慢沉入.我不可能想象D中的A"两份",因为A是空的(没有大小).但后来我记得C++从不创建空类,在我的设置上,例如一个空类的大小为1.然后我能够想象D中的A的"两个副本",现在它开始变得有意义了.
virtual ×10
c++ ×7
c# ×2
inheritance ×2
oop ×2
abstract ×1
base ×1
class ×1
destructor ×1
gcc ×1
object ×1
overloading ×1
overriding ×1
polymorphism ×1
proftpd ×1
sftp ×1
ubuntu ×1
vtable ×1