我有两节课:
class Object {
public:
Object();
virtual void update();
virtual void draw();
private:
protected:
int x, y, tick;
}
Run Code Online (Sandbox Code Playgroud)
和
class Unit : public Object {
public:
Unit();
void update();
private:
protected:
}
Run Code Online (Sandbox Code Playgroud)
然后我在sepparate .cpp文件中定义构造函数和函数.
这是Object的定义:
Object::Object() {
x = y = 0;
};
Object::update() {
tick ++;
};
Object::draw() {
// All my draw code is in here.
};
Run Code Online (Sandbox Code Playgroud)
单位:
Unit::Unit() : Object() {
};
Unit::update() {
Object::update();
// Then there's a bunch of movement related code here.
}; …Run Code Online (Sandbox Code Playgroud) 我已经从我的真实代码编辑了这个,所以它更容易理解.
基类:
class MWTypes
{
public:
virtual long get() { return (0); }
};
Run Code Online (Sandbox Code Playgroud)
派生类:(还有其他类,如char,double等等......)
class TypeLong : public MWTypes
{
public:
TypeLong(long& ref) : m_long(ref) {}
~TypeLong();
long get() { return m_long; }
private:
long& m_long;
};
Run Code Online (Sandbox Code Playgroud)
和存储类:
class RowSet
{
public:
void addElememnt(MWTypes elem);
MWTypes getElement();
std::vector<MWTypes> getVector() { return m_row; }
private:
std::vector<MWTypes> m_row;
};
Run Code Online (Sandbox Code Playgroud)
怎么称呼:
for (i = 0; i < NumCols; i++) // NumCols is 3 on this instance
{
switch(CTypeArray[i]) // this is an …Run Code Online (Sandbox Code Playgroud) 出现问题的情况
请考虑以下c ++代码:
#include <functional>
#include <iostream>
#include <string>
// Superclass
class A {
public:
virtual std::string get() const {
return "A";
}
};
// Subclass
class B : public A {
public:
virtual std::string get() const {
return "B";
}
};
// Simple function that prints the object type
void print(const A &instance) {
std::cout << "It's " << instance.get() << std::endl;
}
// Class that holds a reference to an instance of A
class State {
A &instance; …Run Code Online (Sandbox Code Playgroud) 如果在将此对象赋值给java中的基类类型的变量之后访问派生类对象的字段,则得到预期的行为,即打印字段的派生类的值.在字段的c ++值中,打印属于基类.
在Java中,按预期打印6:
class Ideone
{
static class A {
static int a = 7;
}
static class B extends A {
static int b = 6;
}
public static void main (String[] args) throws java.lang.Exception
{
A a = new B();
System.out.println(((B)a).b);
}
}
Run Code Online (Sandbox Code Playgroud)
在C++中,打印出7个:
#include <iostream>
using namespace std;
class Base {
int i = 7;
public:
Base(){
std::cout << "Constructor base" << std::endl;
}
~Base(){
std::cout << "Destructor base" << std::endl;
}
Base& operator=(const Base& a){
std::cout …Run Code Online (Sandbox Code Playgroud) 我想要一个类型istream可以容纳文件或字符串内容的变量。想法是,如果未指定文件,则将为类型的变量istream分配一个字符串。
std::ifstream file(this->_path)
Run Code Online (Sandbox Code Playgroud)
和
std::istringstream iss(stringSomething);
Run Code Online (Sandbox Code Playgroud)
至
std::istream is
Run Code Online (Sandbox Code Playgroud)
我尝试过istream像将它们分配给变量一样,将它们分配给从同一基类继承的其他对象,但这没有用。
如何分配istringstream和ifstream一个istream变量?
如果我没记错的话,在Java中,我们可以将子类传递给具有超类的函数.代码看起来像这样.
// Assume the classes were already defined, and Apple
// and Pineapple are derived from Fruit.
Fruit apple = new Apple();
Fruit pineapple = new Pineapple();
public void iHaveAPenIHaveAn(Fruit fruit) { ... } // :)
...
public static void main(String[] arg)
{
iHaveAPenIHaveAn(apple); // Uh! Apple-pen.
iHaveAPenIHaveAn(pineapple); // Uh! Pineapple-pen.
}
Run Code Online (Sandbox Code Playgroud)
然而,在C++中,我注意到,从这里,你需要使用基类的引用变量(那是适当的期限?),而不是基类的常规变量.
假设你有两个类:一个基类A和一个A派生类B.
class A { ... };
class B : A { ... };
Run Code Online (Sandbox Code Playgroud)
如果我们有一个neverGonna()接受类A参数的函数,那么为什么函数看起来像这样:
void …Run Code Online (Sandbox Code Playgroud) 我有以下代码:
#include <iostream>
class Base {
public:
virtual void sayHello() {
std::cout << "Hello world, I am Base" << std::endl;
}
};
class Derived: public Base {
public:
void sayHello() {
std::cout << "Hello world, I am Derived" << std::endl;
}
};
void testPointer(Base *obj) {
obj->sayHello();
}
void testReference(Base &obj) {
obj.sayHello();
}
void testObject(Base obj) {
obj.sayHello();
}
int main() {
{
std::cout << "Testing with pointer argument: ";
Derived *derived = new Derived;
testPointer(derived);
}
{
std::cout …Run Code Online (Sandbox Code Playgroud) 假设我有一个Shape基类Circle,Line以及Point派生类。我有两个功能。
std::variant<Circle, Line, Point> process(const Shape &s);
Shape process(const Shape& s);
Run Code Online (Sandbox Code Playgroud)
我可以传入我的任何派生类并在第二个函数中返回一个 Shape 对象,变体只是一个联合,可以在任何给定时间保存我的任何派生类变量。
现在std::variant我还可以使用一个visitor我可以根据我的变体当前持有的类型处理一些函数的地方(我可以创建一个函数对象并传递它std::transform并将其应用于我的每个对象)。但是,我可以virtual在我的基类中创建该函数并让每个派生类实现它。
那么,variant仅仅是一种方便吗?
考虑以下简单代码:
struct Base
{
Base() = default;
Base(const Base&);
Base(Base&&);
};
struct Derived : Base { };
Base foo()
{
Derived derived;
return derived;
}
Run Code Online (Sandbox Code Playgroud)
clang 8.0.0 对此发出警告-Wreturn-std-move:
Run Code Online (Sandbox Code Playgroud)prog.cc:21:10: warning: local variable 'derived' will be copied despite being returned by name [-Wreturn-std-move] return derived; ^~~~~~~ prog.cc:21:10: note: call 'std::move' explicitly to avoid copying return derived; ^~~~~~~ std::move(derived)
但是如果std::move在这里调用代码的行为可能会改变,因为Base对象的子Derived对象将在调用Derived对象的析构函数之前移动,而最后一个的代码将表现不同。
例如看代码(用-Wno-return-std-move标志编译):
#include <iostream>
#include <iomanip>
struct Base …Run Code Online (Sandbox Code Playgroud) 当我学习使用异常时,我刚刚想到了一个问题。代码如下。
// exception constructor
#include <iostream> // std::cout
#include <exception> // std::exception
#include <cxxabi.h>
#define PRINT_TYPENAME(f) std::cout << abi::__cxa_demangle(typeid(f).name(), 0, 0, &status) << std::endl;
struct ooops : std::exception
{
const char *what() const noexcept { return "Ooops!\n"; }
};
int main()
{
ooops e;
std::exception *p = &e;
// demangle the typeid
char *realname;
int status;
PRINT_TYPENAME(p);
PRINT_TYPENAME(*p);
try
{
throw e; // throwing copy-constructs: ooops(e)
}
catch (std::exception &ex)
{
std::cout << ex.what();
}
try
{
throw *p; // …Run Code Online (Sandbox Code Playgroud)