标签: conversion-operator

C++ 重载解析、转换运算符和 const

在这种情况下

void f(int *);
void f(const int *);
...
int i;
f(&i);
Run Code Online (Sandbox Code Playgroud)

情况很清楚 - f(int *) 被调用,这似乎是正确的。

但是,如果我有这个(错误地这样做了(*)):

class aa
{
public:
    operator bool() const;
    operator char *();
};

void func(bool);

aa a;
func(a);
Run Code Online (Sandbox Code Playgroud)

运算符 char *() 被调用。我不明白为什么这样的决策路径比使用 operator bool() 更好。有任何想法吗?

(*) 如果将 const 添加到第二个运算符,则代码当然会按预期工作。

c++ overloading conversion-operator language-lawyer const-method

4
推荐指数
1
解决办法
515
查看次数

使用整数转换重载而不是 bool 转换重载

我想使用 bool cast 重载检查某些内容是否有效:

Menu::operator bool() const {
        bool empty = false;
        if (m_title == nullptr) {
            empty = true;
        }

        return empty;
    }
Run Code Online (Sandbox Code Playgroud)

但是当我使用

if (Menu1) { cout << "valid"; }
Run Code Online (Sandbox Code Playgroud)

它使用 int cast 重载代替

Menu::operator int()
    {
        int choice = option(); 
        return choice;
    }
Run Code Online (Sandbox Code Playgroud)

c++ class constants operator-overloading conversion-operator

4
推荐指数
1
解决办法
164
查看次数

STL:如何为<vector>重载operator =?

有一个简单的例子:

#include <vector>

int main() {
 vector<int> veci;
 vector<double> vecd;

 for(int i = 0;i<10;++i){
  veci.push_back(i);
  vecd.push_back(i);
 }
 vecd = veci; // <- THE PROBLEM
}
Run Code Online (Sandbox Code Playgroud)

我需要知道的是如何重载operator =以便我可以像这样进行赋值:

vector<double> = vector<int>;
Run Code Online (Sandbox Code Playgroud)

我刚尝试了很多方法,但总是编译器返回错误...

是否有任何选项可以使此代码无需更改?我可以写一些额外的行,但不能编辑或删除现有的行.TY.


好的我明白了.我会用另一种方式问你..有没有选择让这段代码无需改变它?我可以写一些额外的行,但不能编辑或删除现有的行.TY.

c++ stl vector operators conversion-operator

3
推荐指数
2
解决办法
2949
查看次数

通过初始标准转换序列区分用户定义的转换序列

该标准似乎提供了两个规则来区分涉及用户定义的转换运算符的隐式转换序列:

13.3.3最佳可行功能[over.match.best]

[...]一个可行的功能F1被定义为比另一个可行的功能F2更好的功能,如果[...]

  • 上下文是用户定义转换的初始化(见8.5,13.3.1.5和13.3.1.6),从返回类型F1到目标类型的标准转换序列(即,被初始化的实体的类型)是比从返回类型F2到目标类型的标准转换序列更好的转换序列.

13.3.3.2对隐式转换序列进行排序[over.ics.rank]

3 - 相同形式的两个隐式转换序列是无法区分的转换序列,除非以下规则之一适用:[...]

  • 用户定义的转换序列U1是比另一个用户定义的转换序列U2更好的转换序列,如果它们包含相同的用户定义的转换函数或构造函数或聚合初始化,并且U1的第二标准转换序列优于第二标准转换序列U2.

据我了解,13.3.3允许编译器区分不同的用户定义的转换运算符,而13.3.3.2允许编译器区分不同的函数(某些函数的重载f),每个函数都需要在参数中进行用户定义的转换(请参阅我的侧边栏给出以下代码(在GCC 4.3中),为什么转换为引用调用两次?).

是否有其他规则可以区分用户定义的转换序列?/sf/answers/96883111/上的答案表明,13.3.3.2:3可以根据隐式对象参数(转换运算符)的cv限定来区分用户定义的转换序列,或者构造函数或聚合初始化的单个非默认参数,但我不知道这是如何相关的,因为这需要比较各个用户定义的转换序列的第一个标准转换序列,标准没有似乎提到了.

假设S1优于S2,其中S1是U1的第一个标准转换序列,S2是U2的第一个标准转换序列,那么U1是否优于U2?换句话说,这段代码是否格式良好?

struct A {
    operator int();
    operator char() const;
} a;
void foo(double);
int main() {
    foo(a);
}
Run Code Online (Sandbox Code Playgroud)

g ++(4.5.1),Clang(3.0)和Comeau(4.3.10.1)接受它,更喜欢非const限定A::operator int(),但我希望它被拒绝为含糊不清且因此格式错误.这是标准中的缺陷还是我对它的理解?

c++ constructor conversion-operator language-lawyer implicit-conversion

3
推荐指数
1
解决办法
928
查看次数

C++多个运算符=()

我正在写一个String类.我希望能够分配我的字符串,如;

a = "foo";
printf(a);
a = "123";
printf(a);
int n = a; // notice str -> int conversion
a = 456; // notice int -> str conversion
printf(a);
Run Code Online (Sandbox Code Playgroud)

我已经为string到整数转换分配了operator =()方法.如何声明另一个operator =()以便我可以执行反向方法?

当我宣布另一个时,它似乎覆盖了前一个.

String::operator const char *() {
    return cpStringBuffer;
}
String::operator const int() {
    return atoi(cpStringBuffer);
}
void String::operator=(const char* s) {
    ResizeBuffer(strlen(s));
    strcpy(cpStringBuffer, s);
}
bool String::operator==(const char* s) {
    return (strcmp(cpStringBuffer, s) != 0);
}

//void String::operator=(int n) {
//  char _cBuffer[33];
//  char* s = itoa(n, …
Run Code Online (Sandbox Code Playgroud)

c++ operators conversion-operator implicit-conversion explicit-conversion

3
推荐指数
1
解决办法
1848
查看次数

C++中的嵌套隐式转换

我希望在两个级别进行隐式转换.以下代码片段是我面临的问题的原型.

//Sources
class A
{
public:
    void print()
    {
        std::cout <<"Class A"<< std::endl;
    }

    operator int()
    {
        return 1;
    }
};

class B
{
public:
    void print()
    {
        std::cout <<"Class B"<< std::endl;
    }

    operator A()
    {
        return A();
    }
};

class C
{
public:
    void print()
    {
        std::cout <<"Class C"<< std::endl;
    }
    operator B()
    {
        return B();
    }
};


void print_(A a)
{
    a.print();
}

//driver

int main( int argc, char* argv[] )
{
    C c;

    //print_( c ); // …
Run Code Online (Sandbox Code Playgroud)

c++ conversion-operator implicit-conversion

3
推荐指数
1
解决办法
158
查看次数

通过转换运算符调用显式实例化的模板函数

让我们假设我们有一个函数模板,它在cpp文件中实现,并提供了明确的实例化,如下所示:

function.h

template<typename T> void function(T val);
Run Code Online (Sandbox Code Playgroud)

function.cpp

#include "function.h"

template<typename T> void function(T val) { /* do something */ }

template void function<double>(double val);
Run Code Online (Sandbox Code Playgroud)

我们现在能够在包含function.h的主文件中调用该函数,如下所示:

double val = 1.0;

function(val);
Run Code Online (Sandbox Code Playgroud)

让我们进一步假设我们有一个类如下所示:

data.h

class Data
    {
    private:
        double mVal;

    public:
        Data(double val) { mVal = val; }

        operator double () { return mVal; }
    };
Run Code Online (Sandbox Code Playgroud)

以下代码导致链接器错误LNK2019:unresolved external(Visual Studio 2010):

Data a(1.0);

function(a);
Run Code Online (Sandbox Code Playgroud)

我们可以使用以下表达式之一来提供一个 to function()

function<double>(a);
function(double(a));
...
Run Code Online (Sandbox Code Playgroud)

但为什么不能只调用函数(a)?如果没有使用类型Data 显式实例化function(),是否存在任何其他解决方案?

c++ templates linker-errors conversion-operator explicit-instantiation

3
推荐指数
1
解决办法
166
查看次数

为什么这个类没有隐式转换为指针?

struct A {
  int i;
};

struct B {
  A a;

  operator A*() { return &a; }
};

int main(int argc, char *argv[])
{
  B b;

  return b->i;
}
Run Code Online (Sandbox Code Playgroud)

g++ 报告 error: base operand of ‘->’ has non-pointer type ‘B’

为什么?我已经想出如何绕过这个问题(使用operator->),但我不明白为什么不进行隐式转换.

c++ conversion-operator

3
推荐指数
1
解决办法
44
查看次数

如何使用显式转换运算符将一个列表转换为另一个列表而不使用扩展方法?

我将此转换运算符添加到我的类中并且运行良好。当我传递一个类的对象时A,它会将其转换为类的对象B

public static explicit operator B(A a)
{
    //Convert A to B
}
Run Code Online (Sandbox Code Playgroud)

但是当我想将 a 转换List<A>为 a时List<B>,它不起作用。我尝试了以下代码,但它也不起作用。

public static explicit operator List<B>(List<A> a)
{
    //Convert List<A> to List<B>
}
Run Code Online (Sandbox Code Playgroud)

相反,它会抛出以下编译器错误:

用户定义的转换必须与封闭类型相互转换。

我不想使用扩展方法来转换它。

c# conversion-operator explicit-conversion

3
推荐指数
1
解决办法
5794
查看次数

为什么转换构造函数优于转换运算符?

我有这个类SmallInt应该表示范围内的正整数值0-255- 包括:

struct SmallInt{
    explicit SmallInt(int x = 0) : iVal_( !(x < 0 || x > 255) ? x :
    throw std::runtime_error(std::to_string(x) + ": value outbounds!")){}
    operator int&() { return iVal_; }
    int iVal_;
};

int main(){

    try{
        SmallInt smi(7);
        cout << smi << '\n';
        cout << smi + 5 << '\n'; // 7 + 5 = 12
        cout << smi + 5.88 << '\n'; // 7.0 + 5.88 = 12.88
        smi = 33; // …
Run Code Online (Sandbox Code Playgroud)

c++ explicit-constructor conversion-operator implicit-conversion

3
推荐指数
1
解决办法
194
查看次数