这是基类:
template <class T>
class DataLogger
{
// ...
public:
void AddData(T Data);
// ...
}
Run Code Online (Sandbox Code Playgroud)
这是派生类:
#include "DataLogger.h"
#include <utility>
class Plotter : public DataLogger<std::pair<long double, long double>>
{
// ...
public:
void AddData(long double x, long double y);
// ...
}
// This method is supposed to wrap the original AddData() method
// which was defined in the base class
void Plotter::AddData(long double x, long double y)
{
AddData(std::make_pair(x, y)); // LINE #29
}
Run Code Online (Sandbox Code Playgroud)
给定的错误是:
第29行:IntelliSense:没有合适的从"std :: pair"到"long …
我对<<派生的clases中的operatorr有一个疑问:
如果我有
class Base
{
//......
friend ostream& operator<<(ostream& out,Base &B)
{
return out<<B.x<<B.y<</*........*/<<endl;
}
//......
};
Run Code Online (Sandbox Code Playgroud)
是下一个可能的?
class Derived: public Base
{
//......
friend ostream& operator<<(ostream& out,Derived &DERIVEDOBJECT)
{
return out<<DERIVEDOBJECT<<DERIVEDOBJECT.nonderivedvar1 <</*.....*/<< endl;
}
}
Run Code Online (Sandbox Code Playgroud)
或者将其DERIVEDOBJECT放入<<操作员不会导致<<重新确定它作为基类的参考?
我有抽象类A,从中继承了许多类.在派生类中,我试图访问A槽A指针中的受保护函数.但是我收到编译器错误.
class A
{
protected:
virtual void f()=0;
};
class D : public A
{
public:
D(A* aa) :mAPtr(aa){}
void g();
protected:
virtual void f();
private:
A* mAPtr; // ptr shows to some derived class instance
};
void D::f(){ }
void D::g()
{
mAPtr->f();
}
Run Code Online (Sandbox Code Playgroud)
编译器错误说:无法访问A类中声明的受保护成员A :: f.
如果我声明mAPtr是D*,而不是A*所有编译.我不明白为什么会这样.
我还在学习和打击派生类.
尝试过一些简单的事情(从我见过的例子):
public class BaseClass
{
public string Title {get;set;}
}
public class Channel : BaseClass
{
public string Path { get; set; }
}
Channel myChannel = new Channel();
myChannel.Title = "hello";
myChannel.Path = "123";
Run Code Online (Sandbox Code Playgroud)
但我myChannel.Path在线上说错了BaseClass does not contain a definition for Path and no extension....
请帮帮我,我做错了什么?
除了简化的通用算法之外,模板在类层次结构上是否有任何好处?例如,在类层次结构中使用模板是否更有效?模板受到程序员对金融工具的高度赞扬,但我并不理解为什么.
在我的代码中,Manager派生自Employee并且每个都有一个operator<<覆盖.
class Employee{
protected:
int salary;
int rank;
public:
int getSalary()const{return salary;}
int getRank()const{return rank;}
Employee(int s, int r):salary(s), rank(r){};
};
ostream& operator<< (ostream& out, Employee& e){
out << "Salary: " << e.getSalary() << " Rank: " << e.getRank() << endl;
return out;
}
class Manager: public Employee{
public:
Manager(int s, int r): Employee(s, r){};
};
ostream& operator<< (ostream& out, Manager& m){
out << "Manager: ";
cout << (Employee)m << endl; //can not compile, …Run Code Online (Sandbox Code Playgroud) 我有一个抽象基类。
我有2个从该基类派生的类。
无论如何,我的类之一可以忽略抽象覆盖用法中的string参数吗? 还是我只需要发送一个空白的邮件而忽略它?(使可读性略有下降)
我是否可以使用一个具有某种可选参数的函数,以便以下两个派生类都可以编译?
PS-以下代码充满了无法编译的代码,作为我要执行的示例
PS PS-是的,我已经编译了以下代码-有关结果,请参见上面的注释
public abstract class MyBaseClass
{ //optional string?
public abstract void FunctionCall(int i, string s = "");
}
public class MyDerivedClass : MyBaseClass
{
public override void FunctionCall(int i)
{
MessageBox.Show(i.ToString());
}
}
public class YourDerivedClass : MyBaseClass
{
public override void FunctionCall(int i, string s)
{
MessageBox.Show(s + " " + i.ToString());
}
}
Run Code Online (Sandbox Code Playgroud) 我正在写一个库,在其中我有一个类BaseClass.使用该库的任何人都将创建自己的继承自BaseClass的类.我有另一个类,我们称之为Manager,它包含一个BaseClass指针的向量,它可以包含从BaseClass派生的任何对象.
Manager类必须处理添加到其BaseClass向量的任何对象的创建和销毁.这是因为可以随时删除向量中的任何对象,也可以删除Manager本身.因此,库的用户无法通过向指向从BaseClass派生的现有对象传递指针来向Manager的baseClass向量添加对象.实际上,我可以允许用户这样做.但这将涉及复制虚拟对象,我宁愿不这样做.
为了解决这个问题,我正在尝试使用模板功能.在尝试将其添加到Manager的向量时,用户应传递从BaseClass派生的对象的类型.这就是我现在拥有的.
//Manager.h
#include <vector>
#include "BaseClass.h"
#include <typeinfo>
class Manager {
//Vector holding pointers to any objects inherited from BaseClass
vector<BaseClass*> baseClasses;
//Template function that needs to add NEW object to baseClass vector
//This I'm having problems with
template<class T>
BaseClass* Add() {
BaseClass* baseClass = new T();
baseClasses.push_back(baseClass);
return baseClass;
}
//Template function that gets object from baseClass vector
//This works fine
template<class T>
BaseClass* Get() {
for (int i = 0; i < baseClasses.size(); i++) { …Run Code Online (Sandbox Code Playgroud) 我有一个名为Graphic_obj的基类.
我有很多派生类,例如:Graphic_obj_text,Graphic_obj_img,...
现在我想要一个包含我所有创建的派生对象的列表.
List<Graphic_obj> obj_list=new List<Graphic_obj>();
Run Code Online (Sandbox Code Playgroud)
现在我的问题出现了..
我想通过我的对象并为每个对象调用"导出"函数,但是从派生类本身!
我不想if/else或切换派生类的所有类型并转换它们..
我会想象这样的事情:
foreach (Graphic_obj obj in obj_list)
{
(obj as obj.GetType()).export_to_csv();
}
Run Code Online (Sandbox Code Playgroud)
但这不会编译..
我可以这样做:
obj.GetType().GetMethod("export_to_csv").Invoke(obj, null);
Run Code Online (Sandbox Code Playgroud)
但我觉得有一种更好更简单的方法吗?
谢谢!
class Base {
public:
virtual Base* clone() const { return new Base(*this); }
// ...
};
class Derived: public Base {
public:
Derived* clone() const override { return new Derived(*this); }
// ...
};
int main() {
Derived *d = new Derived;
Base *b = d;
Derived *d2 = b->clone();
delete d;
delete d2;
}
Run Code Online (Sandbox Code Playgroud)
我在最新版本的Xcode中编译上面的代码,编译器抱怨
cannot initialize a variable of type "Derived*" with an rvalue of type "Base*"*
Run Code Online (Sandbox Code Playgroud)
在Derived *d2 = b->clone().
但我已经克隆了,virtual并让clone()Derived返回 …
derived-class ×10
c++ ×7
c# ×3
base-class ×2
inheritance ×2
templates ×2
abstract ×1
casting ×1
class ×1
comparison ×1
overloading ×1
polymorphism ×1
protected ×1