Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8611505
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T04:23:36+00:00 2026-06-12T04:23:36+00:00

I have a simple wrapper around C null-terminated string, which is essentially a subclass

  • 0

I have a simple wrapper around C null-terminated string, which is essentially a subclass of std::vector< char >. (Yes, I know about std::string, but my wrapper is easier to cooperate with C functions expecting char*. Also, std::string isn’t guaranteed to be contiguous in C++03)

Here is the code:

#include <cstdio>
#include <vector>

typedef std::vector<char> vector_char;
class c_string : public vector_char
{
    public:
    c_string(size_t size) : vector_char(size+1) {}

    c_string(const char* str)
    {
        if(!str) return;
        const char* iter = str;
        do
            this->push_back(*iter);
        while(*iter++);
    }

    c_string() {}

    //c_string(std::nullptr_t) {}

    char* data()
    {
        if(this->size())
            return &((*this)[0]); //line 26
        else
            return 0; 
    }

    const char* data() const { return this->data(); }

    operator char*() { return this->data(); }
    operator const char*() const { return this->data(); }

};

int main()
{
    c_string first("Hello world");
    c_string second(1024);

    printf("%s",first.data());
    printf("%c\n",first[0]);
    snprintf(second, second.size(), "%d %d %d", 5353, 22, 777);
    printf(second);
}

MinGW complains about:

D:\prog\PROJEKTYCPP\hehe_testc_cpp.cpp: In member function 'char* c_string::data()':

D:\prog\PROJEKTYCPP\hehe_testc_cpp.cpp:26:22: warning: ISO C++ says that these are ambiguous, even though the worst conversion for the first is better than the worst conversion for the second: [enabled by default]

In file included from d:\prog\mingw\bin\../lib/gcc/mingw32/4.7.0/include/c++/vector:65:0,
                 from D:\prog\PROJEKTYCPP\hehe_testc_cpp.cpp:2:
d:\prog\mingw\bin\../lib/gcc/mingw32/4.7.0/include/c++/bits/stl_vector.h:768:7:note: candidate 1: std::vector<_Tp, _Alloc>::reference std::vector<_Tp, _Alloc>::operator[]:(std::vector<_Tp, _Alloc>::size_type) [with _Tp = char; _Alloc = std:
:allocator<char>; std::vector<_Tp, _Alloc>::reference = char&; std::vector<_Tp,_Alloc>::size_type = unsigned int]


D:\prog\PROJEKTYCPP\hehe_testc_cpp.cpp:26:22: note: candidate 2: operator[](char
*, int) <built-in>

How can I enforce calling correct overload? Can this problem hurt me silently?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T04:23:38+00:00Added an answer on June 12, 2026 at 4:23 am

    By having an operator char * you’ve provided two ways to do operator[]. The first is std::vector::operator[] applied directly; the second is to convert this to char* and apply [] to that. In this case they both result in the same thing, but the compiler can’t know that.

    Resolve it by specifying explicitly which one you want.

            return &(operator[](0)); //line 26
    

    or

            return &((char*)(*this)[0]); //line 26
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a JTable which is bound to my EventTracker bean, essentially a wrapper
I have a simple wrapper around a WYSIWYG editor (TinyMCE). I'm using JSNI to
I have a simple managed C++ assembly which I am providing unmanaged wrappers for
I have simple class with width and height member fields which define number of
I have a derived class that's a very thin wrapper around a base class.
I want to write a simple wrapper around another class. A small example: class
I am making a simple wrapper for a c library that needs to have
I have a usercontrol wrapper around a DataGridView in a WindowsFormsHost. The wrapper has
I'm trying to build a simple .Net wrapper around some basic C++ code. The
Say I have a simple class AClass with a public member f1 which can

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.