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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T17:59:48+00:00 2026-05-24T17:59:48+00:00

I have a class structure : A <- B where A is base class

  • 0

I have a class structure :

A <- B

where A is base class and B is the derived one.
Some independent code creates object of both these classes.
A has a static variable (lets say ‘static_a’) which is used in A as well as B.

Where should I define this static_a ? When I define that in A.h, it gives me linker error saying multiple definition.
Then I moved it to A.cc file, and compilation passes. But, I am confused whether the class B (which is defined in B.h and B.cc, which doesnt see the definition in A.cc) will get the correct values defined for the variable static_a, as it is defined in A.cc ??

Thanks for the help!

  • 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-24T17:59:48+00:00Added an answer on May 24, 2026 at 5:59 pm

    A static variable definition is a little like declaring an extern variable or a function prototype. It lets the linker know the variable or function exists somewhere in all the compilation units, but it does not create the variable, it just says that it will exist somewhere. It doesn’t make the variable exist.

    When you put the definition in a header file, every file that includes that header will redeclare the variable, and that’s why you get linker errors. You’re creating multiple variables with the same name, so the linker has no idea which one to use.

    When you put an actual definition in a cc file, you’re creating the variable there once, and every time somebody uses it anywhere else across the project, they’re using that one.

    So basically, you’re doing everything right.

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

Sidebar

Related Questions

I have some hierarchy: base, derived classes and some structure storing user data as
I have an entity model where the base class in an inheritance structure has
In building a class structure, I would like to have derived classes potentially posses
I have the following structure: class Base { } class Child : Base {
I have a structure of base class and a couple of inherited classed. Base
I have got the following class structure, and have plenty of classes like C
I have a set of classes which are all derived from a common base
My project structure has these base packages. a.b.c.core a.b.c.web a.b.c.common And core,web,common packages will
I have this code structure: public abstract class ContentEntryBase { public string UniqueIdentifier; public
I have following structure: class User < ActiveRecord::Base has_many :Hobbies, :dependent => :destroy accepts_nested_attributes_for

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.