错误、漏洞、问题 - 页 1588

 

你相信

void f() const

不能改变指针,但可以改变指针所指的对象。那么,在MQL5中没有这样的指针是不能被改变的。因此,const修饰符总是指向指针所指向的对象。

 
Anton Zverev:

那么你认为这里的恒定指针在哪里呢!?

在方法f()和g()中,a 指针成为常数。

那么,在MQL5中没有这样的指针是不能被改变的。因此,const修饰符总是指的是指针所指向的对象

如果没有足够的相关知识,你不应该做出这种断然的结论。 MQL有常数指针,并以与C++相同的方式声明它们。

 
Alexey Navoykov:

在没有充分了解的情况下,你不应该做出这种断然的结论。 在MQL中存在常量指针,其声明方式与C++中相同。

如何在MQL5中声明一个常量指针!?
 

就我记得的开发者的解释而言,恒定性同时适用于对象和指针。

也就是说,在MQL中写const T* === C++中的const T* const

以前是这样的,也许现在不同了,但我怀疑。

 

编译错误

template<typename T>
class A { public:
        bool operator==( const A& ); //error: 'operator' - function must have a body
        T t;
};
class B {
        A<int> a;
};
template<typename T>
bool A::operator==( const A& )  { return false; }
void OnStart()
{
        A<int> a, b;
        Print( a == b );
}
 

编译错误

template<typename T>
class A { public: T t; };
class B { public:
template<typename T> void g(   T  * );
template<typename T> void f( A<T> * );
};
template<typename T> void B::g(   T  * ) {} //нормально
template<typename T> void B::f( A<T> * ) {} //ошибка: 'f' - member function already defined with different parameters
void OnStart()
{
        A<int> a;
        B b;
        b.g( &a );
        b.f( &a );
}
 
A100:

...

template<typename T>
bool A::operator==( const A& )  { return false; }

它应该是这样的。

template<typename T>
bool A<T>::operator==( const A& )  { return false; }
 
Alexey Navoykov:

它应该是这样的。

另一方面,这本来是与C++兼容的--只有B类在那里 "干扰"--如果你去掉它,它的编译不会出错。这就是为什么它的目的是检查
 

编译错误

template<typename T>
class A {};
class B {
        void A() {}
        void f() { A(); } //error: 'A' - template mismatch
};
 

编译错误

template<typename T> class A {};
template<typename T> class B {};
class C {
template<typename T>
        void f( B<A<T> > * ) {}
        B<A<int> > *b;
public:
        void g() { f( b ); } //error: 'f' - cannot to apply function template
};