草庐IT

Private_dirty

全部标签

C++ vector 问题 - 'LNK2001: unresolved external symbol private: static...'

在有人指责我不查看预先存在的问题之前,我已经查看并意识到它与声明有关,但我仍然无法让它工作(可能与我使用vector有关)。Manager.h:#include"Flight.h"#ifndefmanager_h#definemanager_hclassManager{staticvectorairports;staticvectorflights;public:staticvoidloadAirports();staticvoidloadFlights();staticAirportgetAirport(stringcode);staticvectorsplit(conststrin

c++ - 公共(public)算子新建,私有(private)算子删除 : getting C2248 "can not access private member" when using new

一个类有重载的操作符new和delete。new是公开的,delete是私有(private)的。在构造该类的实例时,出现以下错误:pFoo=newFoo(bar)example.cpp(1):错误C2248:'Foo:operatordelete':无法访问在类'Foo'中声明的私有(private)成员但是这里没有调用delete,那么编译器扭曲的头脑中发生了什么?:)错误的原因是什么?是否可以在不借助成员CreateInstance函数的情况下解决问题? 最佳答案 当您执行newFoo()时,会发生两件事:首先调用operat

c++ - 公共(public)算子新建,私有(private)算子删除 : getting C2248 "can not access private member" when using new

一个类有重载的操作符new和delete。new是公开的,delete是私有(private)的。在构造该类的实例时,出现以下错误:pFoo=newFoo(bar)example.cpp(1):错误C2248:'Foo:operatordelete':无法访问在类'Foo'中声明的私有(private)成员但是这里没有调用delete,那么编译器扭曲的头脑中发生了什么?:)错误的原因是什么?是否可以在不借助成员CreateInstance函数的情况下解决问题? 最佳答案 当您执行newFoo()时,会发生两件事:首先调用operat

c++ - 私有(private)成员的常量访问器之间的比较

这个问题的主要部分是关于为类内的私有(private)数据成员创建公共(public)只读访问器的正确且计算效率最高的方法。具体来说,利用consttype&引用来访问变量,例如:classMyClassReference{private:intmyPrivateInteger;public:constint&myIntegerAccessor;//AssignmyPrivateIntegertotheconstantaccessor.MyClassReference():myIntegerAccessor(myPrivateInteger){}};然而,目前解决这个问题的既定方法是利

c++ - 私有(private)成员的常量访问器之间的比较

这个问题的主要部分是关于为类内的私有(private)数据成员创建公共(public)只读访问器的正确且计算效率最高的方法。具体来说,利用consttype&引用来访问变量,例如:classMyClassReference{private:intmyPrivateInteger;public:constint&myIntegerAccessor;//AssignmyPrivateIntegertotheconstantaccessor.MyClassReference():myIntegerAccessor(myPrivateInteger){}};然而,目前解决这个问题的既定方法是利

c++ - 何时/为什么在类里面将函数设为私有(private)?

我什么时候应该创建一个函数private,为什么这是个好主意? 最佳答案 当您不需要其他对象或类来访问该函数时,您应该将函数设为private,而您将从类中调用它。坚持最小权限原则,只允许访问绝对必要的变量/函数。任何不符合此标准的内容都应为private。 关于c++-何时/为什么在类里面将函数设为私有(private)?,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/4505

c++ - 何时/为什么在类里面将函数设为私有(private)?

我什么时候应该创建一个函数private,为什么这是个好主意? 最佳答案 当您不需要其他对象或类来访问该函数时,您应该将函数设为private,而您将从类中调用它。坚持最小权限原则,只允许访问绝对必要的变量/函数。任何不符合此标准的内容都应为private。 关于c++-何时/为什么在类里面将函数设为私有(private)?,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/4505

c++ - 我可以使用 C 风格的转换将派生类转换为私有(private)基类吗?

我可以这样做吗?classA{...};classB:privateA{constA&foo()const{return*((constA*)this);}};我可以采用从基类私有(private)继承的子类并将其转换为基类的公共(public)版本吗?如果没有虚拟方法,我可以这样做吗?我的猜测是肯定的,但我想确保它是安全/便携的。 最佳答案 是的,您可以:标准§5.4/7:...thefollowingstatic_castandreinterpret_castoperations(optionallyfollowedbyacon

c++ - 我可以使用 C 风格的转换将派生类转换为私有(private)基类吗?

我可以这样做吗?classA{...};classB:privateA{constA&foo()const{return*((constA*)this);}};我可以采用从基类私有(private)继承的子类并将其转换为基类的公共(public)版本吗?如果没有虚拟方法,我可以这样做吗?我的猜测是肯定的,但我想确保它是安全/便携的。 最佳答案 是的,您可以:标准§5.4/7:...thefollowingstatic_castandreinterpret_castoperations(optionallyfollowedbyacon

c++ - C++ 私有(private)函数真的需要在头文件中吗?

我一直认为头文件是一种描述类的“公共(public)接口(interface)”,在这种情况下,最好将私有(private)字段和函数保留在.cpp文件中。我知道私有(private)字段需要在标题中,以便其他类可以知道一个类的实例将消耗多少内存,但是当我要编写一个私有(private)帮助函数时,我突然想到这个函数可以设为静态,在这种情况下,它根本不需要成为“类的一部分”,它可以很容易地成为类定义的.cpp文件中的常规函数​​。然后我想到所有私有(private)函数可能会通过接受类字段的指针/引用而不是期望在类中定义而被重写为静态.这将消除在头文件中声明任何私有(private)函