我是C++的新手,对成员变量多态性有疑问.我有以下类定义 -
class Car
{
public:
Car();
virtual int getNumberOfDoors() { return 4; }
};
class ThreeDoorCar : public Car
{
public:
ThreeDoorCar();
int getNumberOfDoors() { return 3; }
};
class CarPrinter
{
public:
CarPrinter(const Car& car);
void printNumberOfDoors();
protected:
Car car_;
};
Run Code Online (Sandbox Code Playgroud)
和实施
#include "Car.h"
Car::Car()
{}
ThreeDoorCar::ThreeDoorCar()
{}
CarPrinter::CarPrinter(const Car& car)
: car_(car)
{}
void CarPrinter::printNumberOfDoors()
{
std::cout << car_.getNumberOfDoors() << std::endl;
}
Run Code Online (Sandbox Code Playgroud)
问题是当我运行以下命令时,会调用父类的getNumberOfDoors.我可以通过使成员变量Car成为指针来解决这个问题,但我更喜欢通过引用而不是指针(我理解为首选)传入输入.你能告诉我我做错了什么吗?谢谢!
ThreeDoorCar myThreeDoorCar;
std::cout << myThreeDoorCar.getNumberOfDoors() << std::endl;
CarPrinter carPrinter(myThreeDoorCar);
carPrinter.printNumberOfDoors();
Run Code Online (Sandbox Code Playgroud) 我正在学习Swift的扩展并遇到一个有点奇怪的问题.
当我在String上编写扩展并将其编译到框架中时,我能够将框架导入到不同的项目中并使用字符串扩展而没有任何问题.但是,当我在NSDate上编写扩展并尝试在不同的项目中使用它时,编译器报告"NSDate没有名为...的成员"
确切地说,我创建了一个非常简单的快速文件,包括这些代码行 -
import Foundation
extension NSDate {
func blah() -> Int {
return 0
}
}
Run Code Online (Sandbox Code Playgroud)
然后我创建了一个目标(Cocoa Framework)并将此文件添加到编译列表中.该框架已成功编译.
然后我创建了一个命令行工具并导入了这个项目,同时链接到框架.当我在NSDate上调用函数blah()时,编译器抱怨.
我正在使用Xcode beta 3.
我正在测试Boost.Python并遇到了一些问题.
我设法完成了"Hello World"示例,没有任何故障(http://www.boost.org/doc/libs/1_54_0/libs/python/doc/tutorial/doc/html/index.html) - 所有编译和我能够在Python中正确使用.so.
但是,只要我将类引入我的测试文件(http://www.boost.org/doc/libs/1_54_0/libs/python/doc/tutorial/doc/html/python/exposing.html),编译器就会编译开始尖叫
Undefined symbols for architecture x86_64:
"boost::python::objects::function_object(boost::python::objects::py_function const&, std::__1::pair<boost::python::detail::keyword const*, boost::python::detail::keyword const*> const&)", referenced from:
boost::python::api::object boost::python::detail::make_function_aux<void (World::*)(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >), boost::python::default_call_policies, boost::mpl::vector3<void, World&, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > >, mpl_::int_<0> >(void (World::*)(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >), boost::python::default_call_policies const&, boost::mpl::vector3<void, World&, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > > const&, std::__1::pair<boost::python::detail::keyword const*, boost::python::detail::keyword const*> const&, mpl_::int_<0>) in SHLibPy.o
boost::python::api::object boost::python::detail::make_function_aux<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > (World::*)(), boost::python::default_call_policies, boost::mpl::vector2<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, World&>, mpl_::int_<0> >(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > (World::*)(), …Run Code Online (Sandbox Code Playgroud)