免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
12下一页
最近访问板块 发新帖
查看: 5805 | 回复: 16
打印 上一主题 下一主题

[C++] 一个C++ string类的问题。 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2007-12-11 00:04 |只看该作者 |倒序浏览
我为了扩充string类的功能写以下代码:
B.CPP
#include "StdAfx.h"
#include<string>
using namespace std;

class B: public string
{
public:
    B(){};

    operator const char*()
    {
        return string::c_str();
    }

    operator char*()
    {
        return (char*)string::data();
    }
};

void fun1(const char *s)
{

}

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

    b1[0] = 'a';   
    fun1(b1);

    return 0;
}

目的:调用以const char*或者char*为参数的函数的时候,类对象可以直接赋值;
编译结果:
B.cpp(30) : error C2666: '[]' : 4 overloads have similar conversions

如果删除operator const char*()和operator char*()成员函数则编译通过;
如果删除b1[0] = 'a';则编译也可以通过。

看情形好像是[] 和 const char* 、char*操作符重载不可以同时使用。但是我写以下一个验证程序

A.CPP
#include "StdAfx.h"
#include <string.h>

class A
{
public:
    A() { memset(m_szBuffer, 0, sizeof(m_szBuffer)); };
    ~A() {};

    operator const char *()
    {
        return m_szBuffer;
    }

    operator char *()
    {
        return m_szBuffer;
    }

    char& operator [](int i)
    {
        return m_szBuffer;
    }
private:
    char m_szBuffer[64];
};

void fun1(const char *s)
{

}

int main(int argc, char* argv[])
{
    A a1, a2;

    a1[0] = 'a';
    fun1(a1);

    return 0;
}

A.cpp文件编译没有任何问题

请高人来解答解答,这是什么原因? 为什么在类B中[] 和 const char* 、char*操作符重载不可以同时使用;
而在A类中可以???

[ 本帖最后由 gosoft 于 2007-12-11 00:13 编辑 ]

论坛徽章:
0
2 [报告]
发表于 2007-12-11 09:29 |只看该作者
代码引起歧义了,这也是string为啥没有operator char*之类的运算符了

  1. #include<string>
  2. using namespace std;

  3. class B: public string
  4. {
  5. public:
  6.     B(){};

  7.     operator const char*()const
  8.     {
  9.         return string::c_str();
  10.     }

  11.     operator char*()
  12.     {
  13.         return (char*)string::data();
  14.     }
  15. };
  16. void fun1(const char *s)
  17. {
  18. }
  19. int main(int argc, char* argv[])
  20. {
  21.     B b1, b2;
  22.         b1[(unsigned int)0]='a';
  23.     fun1(b1);
  24.     return 0;
  25. }
复制代码

论坛徽章:
0
3 [报告]
发表于 2007-12-11 14:24 |只看该作者
请高手说说那里具体歧义了!!

论坛徽章:
0
4 [报告]
发表于 2007-12-11 14:48 |只看该作者
不是我说的,是编译器说的

vc8的报错信息
error C2666: 'std::basic_string<_Elem,_Traits,_Ax>:perator []' : 4 overloads have similar conversions
        with
        [
            _Elem=char,
            _Traits=std::char_traits<char>,
            _Ax=std::allocator<char>
        ]
        C:\Program Files\Microsoft Visual Studio 8\VC\INCLUDE\xstring(1547): could be 'const char &std::basic_string<_Elem,_T
raits,_Ax>:perator [](unsigned int) const'
        with
        [
            _Elem=char,
            _Traits=std::char_traits<char>,
            _Ax=std::allocator<char>
        ]
        C:\Program Files\Microsoft Visual Studio 8\VC\INCLUDE\xstring(1527): or 'char &std::basic_string<_Elem,_Traits,_Ax>::
operator [](unsigned int)'
        with
        [
            _Elem=char,
            _Traits=std::char_traits<char>,
            _Ax=std::allocator<char>
        ]
        or 'built-in C++ operator[(const char *, int)'
        or 'built-in C++ operator[(char *, int)'
        while trying to match the argument list '(B, int)'
MSDN中关于 C2666 的解释

编译器错误 C2666
错误消息
“identifier”: number 个重载有相似的转换

重载函数或运算符不明确。形参列表可能太相似,编译器无法解析多义性。若要消除此错误,请显式强制转换一个或多个实参。

论坛徽章:
0
5 [报告]
发表于 2007-12-11 14:51 |只看该作者
顺便帖一下g++的报错信息

q.cpp:29: error: ISO C++ says that these are ambiguous, even though the worst conversion for the first is better than the wo
st conversion for the second:
d:\codeblocks\bin\../lib/gcc/mingw32/4.2.1-sjlj/include/c++/bits/basic_string.h:707: note: candidate 1: typename _Alloc::reb
nd<_CharT>:ther::reference std::basic_string<_CharT, _Traits, _Alloc>:perator[](typename _Alloc::rebind<_CharT>:ther::
ize_type) [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>]
q.cpp:29: note: candidate 2: operator[](char*, int) <built-in>
q.cpp:29: error: ISO C++ says that these are ambiguous, even though the worst conversion for the first is better than the wo
st conversion for the second:
d:\codeblocks\bin\../lib/gcc/mingw32/4.2.1-sjlj/include/c++/bits/basic_string.h:707: note: candidate 1: typename _Alloc::reb
nd<_CharT>:ther::reference std::basic_string<_CharT, _Traits, _Alloc>:perator[](typename _Alloc::rebind<_CharT>:ther::
ize_type) [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>]
q.cpp:29: note: candidate 2: operator[](const char*, int) <built-in>

论坛徽章:
0
6 [报告]
发表于 2007-12-11 15:27 |只看该作者
#include<string>
using namespace std;

class B: public string
{
public:
    B(){};

    operator const char*()
    {
        return string::c_str();
    }

    operator char*()
    {
        return (char*)string::data();
    }

        char& operator [](int i){
                return string:perator[](i);       
        }
};

void fun1(const char *s)
{
        printf("%s\n", s);
}

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

    b1[0] = 'a';   
    fun1(b1);

    return 0;
}

论坛徽章:
0
7 [报告]
发表于 2007-12-11 16:29 |只看该作者
不建议重载STL的容器,具体原因见EC++ Item 14.

论坛徽章:
0
8 [报告]
发表于 2007-12-11 16:56 |只看该作者
为什么yjm0573 重载的[]运算符就可以了?

和basic_string的定义也几乎一样啊:
_Ty ==> char
typedef _Ty _FARQ& reference;
reference operator[](size_type _P0)

论坛徽章:
0
9 [报告]
发表于 2007-12-11 17:11 |只看该作者
int main(int argc, char* argv[])
{
    B b1, b2;

    b1[0] = 'a';   
// char * p = b1; //operator char*
//  p[0] = 'a';      //operator []
    fun1(b1);

    return 0;
}
试试 这个:
class B: public string
{
public:
    B(){};

    operator const char*()
    {
        return string::c_str();
    }

    operator char*()
    {
        return (char*)string::data();
    }

   
};

void fun1(const char *s)
{
        printf("%s\n", s);
}

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

    string p = b1;
    p[0] = 'a';   
    fun1(b1);

    return 0;
}

论坛徽章:
0
10 [报告]
发表于 2007-12-11 23:53 |只看该作者
顶顶顶顶顶顶顶顶顶顶顶顶顶
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP