我曾经在一个类上工作并开始在同一个.cpp文件中编写所有内容.但是,过了一段时间我可以看到这个类越来越大,所以我决定把它分成.h和.cpp文件.
gaussian.h文件:
class Gaussian{
private:
double mean;
double standardDeviation;
double variance;
double precision;
double precisionMean;
public:
Gaussian(double, double);
~Gaussian();
double normalizationConstant(double);
Gaussian fromPrecisionMean(double, double);
Gaussian operator * (Gaussian);
double absoluteDifference (Gaussian);
};
Run Code Online (Sandbox Code Playgroud)
gaussian.cpp文件:
#include "gaussian.h"
#include <math.h>
#include "constants.h"
#include <stdlib.h>
#include <iostream>
Gaussian::Gaussian(double mean, double standardDeviation){
this->mean = mean;
this->standardDeviation = standardDeviation;
this->variance = sqrt(standardDeviation);
this->precision = 1.0/variance;
this->precisionMean = precision*mean;
}
//Code for the rest of the functions...
double absoluteDifference (Gaussian aux){
double absolute = abs(this->precisionMean - aux.precisionMean); …Run Code Online (Sandbox Code Playgroud) 我知道关键字在封装方面的一般用例,friend但有几次,我需要关键字friend只是为了“完成工作”。这些用例并不让我高兴,所以我想知道是否有一些替代方案。这是第一个最小的例子:
struct Foo{
enum class Bar{
a=1,b=2,c=4
};
// need to tell the compiler of operator| before it gets used
// but it can't be a member function of Foo: so add friend keyword
friend Bar operator|(const Bar& b1, const Bar& b2);
// constructor needs a default value using
// operator| for Bars
Foo( Bar b = Bar::a | Bar::b );
};
// definition of operator|, etc.
Run Code Online (Sandbox Code Playgroud)
在构造函数声明中给出默认值之前,编译器有什么方法可以查看接口内部嵌套类的声明吗?operator|Foo …
c++ templates operator-overloading friend non-member-functions
我有一个Matrix类,它有*标量和矩阵乘法的重载运算符.
template <class T> class Matrix
{
public:
// ...
Matrix operator*(T scalar) const;
// ...
}
// ...
template <class T>
Matrix<T> Matrix<T>::operator*(T RightScalar) const
{
Matrix<T> ResultMatrix(m_unRowSize, m_unColSize);
for (uint64_t i=0; i<m_unRowSize; i++)
{
for (uint64_t j=0; j<m_unColSize; j++)
{
ResultMatrix(i, j) = TheMatrix[m_unColSize * i + j] * RightScalar;
}
}
return ResultMatrix;
}
// ...
Run Code Online (Sandbox Code Playgroud)
我可以将矩阵对象与右侧的标量相乘而没有任何问题:
Matrix<double> X(3, 3, /* ... */); // Define a 3x3 matrix and initialize its contents
Matrix<double> …Run Code Online (Sandbox Code Playgroud) c++ templates operator-overloading member-functions non-member-functions
考虑重载加法运算的遗留类模板+=和+
template<class T>
class X
{
public:
X() = default;
/* implicict */ X(T v): val(v) {}
X<T>& operator+=(X<T> const& rhs) { val += rhs.val; return *this; }
X<T> operator+ (X<T> const& rhs) const { return X<T>(*this) += rhs; }
private:
T val;
};
Run Code Online (Sandbox Code Playgroud)
在代码审查时,观察到它+是可实现的+=,为什么不使它成为非成员(并保证左右参数的对称性)?
template<class T>
class X
{
public:
X() = default;
/* implicit */ X(T v): val(v) {}
X<T>& operator+=(X<T> const& rhs) { val += rhs.val; return *this; }
private: …Run Code Online (Sandbox Code Playgroud) c++ operator-overloading implicit-conversion argument-dependent-lookup non-member-functions
我有一个课程模板Foo<T>.
我想实现一个非成员函数Bar,它需要两个Foos并返回一个Foo.我想Bar成为一名非会员,因为写作者Bar(f1, f2)比写作更自然f1.Bar(f2).我也想Bar成为inline因为计算是微不足道的.
template <typename T>
inline Foo<T> Bar(const Foo<T> &lhs, const Foo<T> &rhs) {
...
}
Run Code Online (Sandbox Code Playgroud)
诀窍是Bar需要访问Foo私人数据.我不希望拥有私有数据的访问者 - 没有充分的理由将私有数据暴露给用户.所以我想结交Bar一个朋友Foo.
template <typename T>
class Foo {
...
private:
T w, x, y, z;
friend Foo<T> Bar(const Foo<T> &lhs, const Foo<T> &rhs);
};
Run Code Online (Sandbox Code Playgroud)
这是我遇到麻烦的地方.编译器抱怨:
当友元声明引用功能模板的特化时,不能使用内联说明符.
这个规则是由标准强加的还是特定于MSVC++?
这是我尝试过的:
创建Bar一个const公共成员函数,然后声明一个简单返回的非成员版本lhs.Bar(rhs).这似乎是最不讨厌的解决方案.
删除inline提示,知道编译器将决定内联而不管提示.这是否会违反单一定义规则?它仍然必须在头文件中定义,因为它是一个函数模板. …
每当我有一些朝向"效用"方向的功能时,我最终想知道哪个选项是最好的.例如,在我工作的上下文中打印消息结构(自己的或外部的),一些编码/解码代码或简单的一些有用的转换函数.
我想到的选项是:
1)helper类/ struct中的静态函数.
struct helper
{
static bool doSomething(...);
};
Run Code Online (Sandbox Code Playgroud)
2)非成员函数.
namespace helper
{
bool doSomething(...);
}
Run Code Online (Sandbox Code Playgroud)
3)静态非成员函数.
namespace helper
{
static bool doSomething(...);
}
Run Code Online (Sandbox Code Playgroud)
在某些情况下,可能需要在"实用程序"中初始化或保持状态,因此我选择选项1以避免"全局"状态.但是,如果没有需要保留的状态,我应该选择2还是3?选项2和3之间的实际区别是什么?
重要的是要考虑什么,是否有一种首选的方法来解决这个问题?谢谢!
为什么这些C++ 11头的新的功能<string>(stod,stof,stoull)所述的不成员函数string类?
是不是更符合C++ mystring.stod(...)而不是stod(mystring,...)?
非成员函数模板begin(container)和end(container)C++ 0x的一部分吗?如果是这样,他们居住在哪个头文件中?
背景资料
我已经用Java编程了一段时间,而且我几个月前才切换到C++,所以如果答案只是我错过的傻话,我会道歉!现在已经说过了,现在是时候解决问题吧!我正在开发一个基于文本的基本游戏引擎,最近我遇到了一个有趣的具体且不太可能的问题.我尝试在下面的程序中以较小的比例测试它,并决定只显示(而不是我的实际游戏代码),以免屏蔽屏幕,并使问题更少复杂.下面建模的问题反映了我的实际代码的问题,只是没有蓬松的干扰者.
问题
本质上问题是多态性之一.我想重载输出操作符"<<"以作为显示功能,该功能对于层次结构中的每个对象都是唯一的.问题是,当我从存储这些层次结构成员的列表中调用此运算符时,它们将丢失其标识并调用基类的输出运算符.通常,人们可以通过使用简单的显示方法替换运算符重载,将显示方法标记为虚拟,并继续他们快乐的一天来解决这个问题.我并不特别介意对代码进行更改,但现在我只是好奇.有没有办法在层次结构中重载运算符,导致我在这里得到什么?
[示例]代码
#include <vector>
#include <iostream>
#include <string>
using namespace std;
class Polygon {
friend ostream& operator<<(ostream& os, const Polygon& p);
public:
private:
};
class Rectangle : public Polygon {
friend ostream& operator<<(ostream& os, const Rectangle& r);
public:
private:
};
class Square : public Rectangle {
friend ostream& operator<<(ostream& os, const Square& s);
public:
private:
};
ostream& operator<<(ostream& os, const Polygon& p) {
os << "Polygon!" << endl;
return os;
}
ostream& operator<<(ostream& os, const Rectangle& r) …Run Code Online (Sandbox Code Playgroud) c++ polymorphism operator-overloading non-member-functions object-slicing
情况是某些成员函数bar::Bar::frobnicate想要利用ADL从具有相同名称的函数内的某个未知命名空间中查找函数.但是,它只找到自己的名字.
(请注意,实际上,这Bar是一个Foo不可知的模板;这只是可重现的,最小的测试用例)
namespace foo {
struct Foo {};
void frobnicate(Foo const &) {}
}
namespace bar {
struct Bar {
void frobnicate() {
foo::Foo foo;
frobnicate(foo); // <-- error
}
};
}
int main () {
bar::Bar x;
x.frobnicate();
frobnicate(foo::Foo());
}
Run Code Online (Sandbox Code Playgroud)
结果是:
test.cc: In member function ‘void bar::Bar::frobnicate()’:
test.cc:10:31: error: no matching function for call to ‘bar::Bar::frobnicate(foo::Foo&)’
test.cc:10:31: note: candidate is:
test.cc:8:18: note: void bar::Bar::frobnicate()
test.cc:8:18: note: candidate expects 0 arguments, …Run Code Online (Sandbox Code Playgroud) c++ interface argument-dependent-lookup non-member-functions
c++ ×10
c++11 ×2
friend ×2
templates ×2
containers ×1
interface ×1
iterator ×1
polymorphism ×1
static ×1
this ×1