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 6844147
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T00:20:25+00:00 2026-05-27T00:20:25+00:00

concerning the following piece of code : StaticEnum.h #pragma once #include <string> class StaticEnum

  • 0

concerning the following piece of code :

StaticEnum.h

#pragma once

#include <string>

class StaticEnum
{
public:
    static StaticEnum const Test;
    operator std::string const() {return myDir;}
private:
    StaticEnum(std::string const & dir_in):myDir(dir_in){}
    std::string myDir;
};

StaticEnum.cpp

#include "StaticEnum.h"

StaticEnum const StaticEnum::Test("patient");

main.cpp

#include "StaticEnum.h"

int main()
{
    StaticEnum a = StaticEnum::Test;
    StaticEnum b(StaticEnum::Test);

    std::string aStr = static_cast<std::string>(a);
    std::string bStr = static_cast<std::string>(b);

    std::cout << "Test a : " << static_cast<std::string>(a) << std::endl;
    std::cout << "Test b : " << static_cast<std::string>(b) << std::endl;
    //std::cout << "Test direct : " << static_cast<std::string>(StaticEnum::Test); Error    1   error C2440: 'static_cast' : cannot convert from 'const StaticEnum' to 'std::string'
}

Why calling the conversion operator directly results in an error, while when using a temporary variable to store the static variable works? Is this some violation of a rule ? I don’t get it!

Edit: Using MSVS 2008, no external lib available.

Thanks in advance.

  • 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-05-27T00:20:26+00:00Added an answer on May 27, 2026 at 12:20 am

    Your operator const std::string () is not const qualified. So only non const objects can call it. StaticEnum::Test is a const object. To solve this problem, do following:

    operator std::string const() const {return myDir;}
                                 ^^^^^
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Recently I attended a lecture concerning some design patterns: The following code had been
My question is concerning SQL connection status, load, etc. based on the following code:
I need help concerning following code: http://jsfiddle.net/8PsdE/ <SELECT ID=pizzasize NAME=pizzasize> <OPTION VALUE=s>small <OPTION VALUE=m>medium
I have the following JQuery Code concerning my tabs : $(#onglet>ul>li).click(function(){ $(#onglet ul li).removeClass('Selectionne').addClass(OngletPrincipal);
Imagine the following simple code: public void F<T>(IList<T> values) where T : struct {
I have the following questions concerning code generation for Hibernate annotated DTO/DAO-s: Is it
I've got the following piece of code which exectues within no problem SELECT (
I have the following piece of code to calculate how far back in the
i have the following problem concerning the index:: my source code:: <table style=width: 80%>
The following Code System.out.println(Start); String s = ; //936 * 5 = 4680 characters

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.