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

  • Home
  • SEARCH
  • 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 8412399
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T00:39:41+00:00 2026-06-10T00:39:41+00:00

I have my own class that represents a custom string class. I’m using VS2012RC.

  • 0

I have my own class that represents a custom string class. I’m using VS2012RC. I have overloaded some operators of my class CustomString.

Here’s some code:

CustomString::CustomString(string setstr)
{           
    str = setstr;
}

CustomString::operator const char *()
{   
    return (this->str.c_str());
}

CustomString &CustomString::operator = (char *setstr)
{
    str = setstr;
    return *this;
}

I can define my object and use it like this:

CustomString str = "Test string";

and i can print the result as:

printf(str);

printf((string)(str).c_str());

printf((string)(str).data());

printf("%s\n",(string)(str).c_str());

printf("%s\n",(string)(str).data());

And there is not any error.

But if i use it like this:

printf("%s\n", str);

There is an exception in msvcr110d.dll (error in memory access)

Why printf(str) is ok, but printf(“%s\n”,str) is not ok?

How can i modify my code to use printf(“%s\n”,str) ?

…

After hours of googling, I found that explict cast (string), static_cast (str) and _str() method are add a null-terminated chars: ‘\0’;

i’ve modified my code as:

printf("%s\n",str + '\0');

and it’s worked!

Is there any way to modify my custom constructor to add a null-terminated string and pass a correct value with null-terminated chars to get working the following code:

printf("%s\n",str);
  • 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-10T00:39:42+00:00Added an answer on June 10, 2026 at 12:39 am

    You can’t (at least not in a portable way). printf looks at the object passed as parameter and treats it as a %s, which is a char array. You run into undefined behavior. Also, the parameters passed to printf are, sort of say, type-less.

    Why printf(str) is ok?

    Because the first parameter is types, and is a const char*. The implicit cast is made via your operator. The rest of the parameters don’t behave the same.

    I’d use cout instead, and overload operator << (ostream&, const CustomString&).

    Don’t do this:

    I said you can’t, in a portable way. For a class like

    class CustomString
    {
       char* str;
       //...
    };
    

    that might work, because of how classes are represented in memory. But, again, it’s still undefined behavior.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here is the context. I have a class in my project that represents a
I have a class that defines its own enum like this: public class Test
I have a class that is its own activity that basically i use to
I have create my own NSOpenGLView class, right now the data that i want
I have a class called Flamethrower which naturally has its own ammunition that is
I have a class that represents credit card details. To represent valid from and
I have a class that represents a row in a table. I want to
Does android have a built-in class that represents 2D vector? I need to do
I'm trying to hide window after its startup. I have own window-class which is
I have made my own exception class which derives from runtime_error and is getting

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.