What is the proper signature of the main
function in C++? What is the correct return type, and what does it mean to return a value from main
? What are the allowed parameter types, and what are their meanings?
这是系统特定的吗?这些规则会随着时间而改变吗?如果我违反它们会发生什么?
可能重复:
学习C++:多态和切片
这是我之前提出的一个问题.这些类看起来像这样:
class Enemy
{
public:
void sayHere()
{
cout<<"Here"<<endl;
}
virtual void attack()
{
}
};
class Monster: public Enemy
{
public:
void attack()
{
cout<<"RAWR"<<endl;
}
};
class Ninja: public Enemy
{
public:
void attack()
{
cout<<"Hiya!"<<endl;
}
};
Run Code Online (Sandbox Code Playgroud)
我是C++的新手,我很困惑为什么这只能用指针(忍者和怪物都来自敌人):
int main()
{
Ninja ninja;
Monster monster;
Enemy *enemies[2];
enemies[0] = &monster;
enemies[1] = &ninja;
for (int i = 0; i < 2; i++)
{
enemies[i]->attack();
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
为什么我不能这样做呢?:
int main()
{
Ninja ninja;
Monster …
Run Code Online (Sandbox Code Playgroud) 我有一个时间了解参考的魔鬼.请考虑以下代码:
class Animal
{
public:
virtual void makeSound() {cout << "rawr" << endl;}
};
class Dog : public Animal
{
public:
virtual void makeSound() {cout << "bark" << endl;}
};
Animal* pFunc()
{
return new Dog();
}
Animal& rFunc()
{
return *(new Dog());
}
Animal vFunc()
{
return Dog();
}
int main()
{
Animal* p = pFunc();
p->makeSound();
Animal& r1 = rFunc();
r1.makeSound();
Animal r2 = rFunc();
r2.makeSound();
Animal v = vFunc();
v.makeSound();
}
Run Code Online (Sandbox Code Playgroud)
结果是:"树皮树皮rawr rawr".
用Java的思维方式(显然已经破坏了我对C++的概念化),结果将是"树皮树皮树皮".我从前一个问题中了解到,这种差异是由切片引起的,我现在对切片的理解有了很好的理解.
但是,让我们说我想要一个返回Animal值的函数,它实际上是一个Dog. …
我在下面有一个特定的场景。下面的代码应该打印 B 和 C 类的 'say()' 函数并打印 'B say..' 和 'C say...' 但它没有。任何想法.. 我正在学习多态,所以也有评论在下面的代码行中与它相关的几个问题。
class A
{
public:
// A() {}
virtual void say() { std::cout << "Said IT ! " << std::endl; }
virtual ~A(); //why virtual destructor ?
};
void methodCall() // does it matters if the inherited class from A is in this method
{
class B : public A{
public:
// virtual ~B(); //significance of virtual destructor in 'child' class
virtual void say () { // …
Run Code Online (Sandbox Code Playgroud) 假设以下代码:
class Event {
public:
virtual void execute() {
std::cout << "Event executed.";
}
}
class SubEvent : public Event {
void execute() {
std::cout << "SubEvent executed.";
}
}
void executeEvent(Event e) {
e.execute();
}
int main(int argc, char ** argv) {
SubEvent se;
executeEvent(se);
}
Run Code Online (Sandbox Code Playgroud)
执行时,程序输出"Event executed.",但我想执行SubEvent.我怎样才能做到这一点?
为什么此C ++代码不起作用?它旨在将基类动态转换为派生类。我将如何实现?
class base {
public:
int x = 0;
};
class a : public base {
public:
char c = 'a';
};
class b : public base {
public:
long int d = 'b';
};
std::vector<base> vec;
for (int i = 0; i < 5; i++) {
b temp;
vec.push_back(temp);
}
for (int i = 0; i < 5; i++) {
b* temp = (b*)&vec[i];
std::cout << temp->d << std::endl;
}
Run Code Online (Sandbox Code Playgroud) 我想比较两个派生自相同基类型但不是相同派生类的对象.所以我将==运算符设为虚拟并在派生类中覆盖它.
当我将所有对象存储在基类型的数组中时,不会调用派生的实现,而是直接进入基本实现.但是,当数组是指向基类的类型指针并且取消引用元素时,它确实有效.
请问一些人请解释为什么会出现这种情况?它让我困惑;-)
enum eType {
BASE_TYPE,
DERIVED_TYPE
};
class A {
eType mType;
public:
A() : mType(BASE_TYPE) {}
A(eType Type) : mType(Type) {}
virtual bool operator == (A &Other) {
return mType == Other.mType;
}
};
class B : public A {
int bVal;
public:
B(int Val) : A(DERIVED_TYPE), bVal(Val) {}
virtual bool operator == (A &Other) {
if(!(A::operator ==(Other))) {
return false;
}
B* myB = (B*)&Other;
return bVal == myB->bVal;
}
};
int main(int argc, char *argv[]) …
Run Code Online (Sandbox Code Playgroud) 我已将面向对象的类集成到我的OpenGL应用程序中.基类有一个被调用的函数Tick
,应该使用表示增量时间的参数调用每个tick(显而易见).这是它的样子(没有这个问题的无关紧要的东西).
头:
class Object
{
public:
virtual void Tick(float DeltaTime);
}
class Controller : public Object
{
public:
virtual void Tick(float DeltaTime);
}
Run Code Online (Sandbox Code Playgroud)
然后我有一个名为的类Engine
包含主循环,初始化和处理(将其与窗口创建分开).
在那个课程中我需要跟踪游戏中的所有对象,所以我创建了一个数组:
Object* Objects = new Object[10]; // for now max 10 objects
然后调用tick函数,我通过数组迭代:
for (unsigned int c = 0; c < 10; c++)
Objects[c].Tick(delta);
Run Code Online (Sandbox Code Playgroud)
被delta
循环之前进行计算.
问题在于,如果我将数组中的一个对象分配给Controller
(例如),则调用的Tick函数始终是在Object
实际存储的类中而不在其中的那个.
我甚至尝试过类型转换(每个类都有一个字符串来识别它的类型),但没有用.
我很确定答案是显而易见的(就像我上一期的答案一样),但我找不到答案.
感谢您的时间.:)