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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T14:53:35+00:00 2026-06-01T14:53:35+00:00

I have three files, a main .cpp file: #include <stdio.h> #include myClass.h int main()

  • 0

I have three files, a main .cpp file:

#include <stdio.h>
#include "myClass.h"



int main()
{

    myClass mvar;

    tryVar = 23; // why does this not work?

    printf("%d ", mvar.readTryVar()); // This writes out 0, why??

    return 0;
}

a myClass.cpp file

#include "myClass.h"


myClass::myClass(void)
{
}


myClass::~myClass(void)
{
}

void myClass::setTryVar()
{
    tryVar = 23334;
}

int myClass::readTryVar()
{
    return tryVar;
}

and a myClass.h file

#pragma once

static int tryVar;

class myClass
{
public:
    myClass(void);
    ~myClass(void);


    void setTryVar();

    int readTryVar();
};

They’re very simple files, however I can’t understand why the static variabile isn’t set in the main function and I need to set it through the myClass functions.

I think that I don’t know very well how the “translation units” are created, I know that the “include” directive simply copies the content of the header file into the .cpp file before the actual compilation.. then why isn’t the static variable visible?

  • 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-01T14:53:36+00:00Added an answer on June 1, 2026 at 2:53 pm

    static has multiple meanings. Outside a class, it declares a variable that is unique to every translation unit, so main.cpp and myClass.cpp have their own copies.

    To accomplish what you want, you need an extern variable:

    //myClass.h
    extern int tryVar;
    
    //myClass.cpp
    int tryVar = 0;  //definition needed for extern variable
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose that I have those three files: a.h //a.h header #include <stdio.h> int int_variable;
I have three files: movie.h (header); movie.cpp (implementation file); lab9.cpp (driver file using movie
I have the following three files in the same directory: citysim.cpp #include utils.h using
I have three files: Main, slist.h and slist.cpp can be seen at http://forums.devarticles.com/c-c-help-52/confused-why-i-am-getting-c2143-and-c1010-error-259574.html I'm
In Unix, I have got three main files. One of them is a library
I have a struct defined in a header file with three other files that
I have 3 files: main .h file - contains implementation of a simple WinForm
I have one helper file included in two main files which are built into
Here's the situation: I have three files, Test1.cpp and Test2.cpp. Test1.cpp can be compiled
i have this files: //Q2a.h #ifndef Q2A_H #define Q2A_H inline int MyFactorial(int a) {

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.