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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T16:17:12+00:00 2026-05-22T16:17:12+00:00

I have a class in MyClass.h file: // MyClass.h #ifndef __MY_CLASS_H__ #define __MY_CLASS_H__ #include

  • 0

I have a class in MyClass.h file:

// MyClass.h
#ifndef __MY_CLASS_H__
#define __MY_CLASS_H__

#include <string>

class MyClass
{
    static const std::string MyStaticConstString; // I cannot initialize it here, because it's not an integral type.
};

// OK, let's define/initialize it out side of the class declaration
// static
const std::string MyClass::MyStaticConstString = "Value of MyStaticConstString";

#endif

The problem is, the compiler will complain “Multiple definition” if this file is included for more than one times.

So I have to move the definition of MyStaticConstString to MyClass.cpp file. But what if MyClass is part of a library, and I want my users to see the const static value in the MyClass.h file, which makes sense, because it’s a static const value.

How should I do? I hope I made myself clear.

Thanks.

Peter

  • 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-22T16:17:13+00:00Added an answer on May 22, 2026 at 4:17 pm

    No, for the same reason you can’t put global variables in headers, const-qualified or not. Document your constant instead (if it’s a constant, then why should users care about its value anyway?).

    Also, don’t prepend your identifiers with underscores (__MY_CLASS_H__), they’re reserved for implementation stuff.

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

Sidebar

Related Questions

I have a ruby file as follows: module Example class Myclass def t_st Hello
I have a class like this: class MyClass{ public: MyClass(int Mode); private: std::map <
Suppose I have a class public class MyClass { private Set<String> set = new
Say I have class myClass in file myClass.py and I want to be able
I have a class named MyClass. And in the file MyClass.m I start the
Imagine we have a class public class MyClass { private string _val; public MyClass(string
I have a class like this: class MyClass { Map<String, String[]> arrays; public void
I have this following class: Class myclass{ public static $item_row_name = array( array('Item 1','Item_1','Help
I have similar matlab and java classes: public class myClass { double[][] v1; String
I have the following constructor for my Class public MyClass(File f1, File f2, File

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.