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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T06:46:01+00:00 2026-06-03T06:46:01+00:00

Maybe you will be able to clear something for me, because I don’t know

  • 0

Maybe you will be able to clear something for me, because I don’t know exactly where my thinking is flawed. First some code:

Talker.h:

class talker       
{
    public:
    talker();
    void sayHello() {cout << "Hello";} ;
};

anotherClass.h:

class anotherClass
{
    public:
    anotherClass();
    void doSomethingYourself() { cout << "will do"; };
    void askHimToSayHello() { pointerToTalker->sayHello; };  
                          //Access violation, bad pointer(?)
};

common.h:

 static talker *pointerToTalker;       
      // if I add here "= new talker", code works fine

main.cpp:

#include "common.h"
int main()
{
    pointerToTalker = new talker;            // Here is the bug, but why?
    pointerToTalker -> sayHello;             // says Hello alright

    anotherClass *pointerToAnotherClass = new anotherClass;
    pointerToAnotherClass -> doSomething ();  //Does something OK

    pointerToAnotherClass -> askHimToSayHello(); // Causes access violation 
}

Of course functions are a bit more complex, and each is implemeted in coresponding .cpp including “common.h”. My question is – why pointerToTalker, if initialized inside main() does not work inside anotherClass::AskHimToSayHello()? It should pointing to valid memory by the time it is used there.
It is my “Hello world, OOP!” btw, so please be gentle if there is no hope for me 🙂

Sorry for the childish style btw. It helps me cut down the code I have to something more compact without me loosing the big picture :).

  • 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-03T06:46:03+00:00Added an answer on June 3, 2026 at 6:46 am

    Because

    static talker *pointerToTalker;   
    

    is not a global. In this context, static gives the variable internal linkage for each translation unit (cpp file + included files) in which common.h is included.

    You need to declare it as extern:

    extern talker *pointerToTalker;   
    

    and initialize it in a single implementation file.

    Declaring it static will create a copy of pointerToTalker for each translation unit. So you’re initializing the one from main.cpp. Others are left uninitialized, and thus you run into undefined behavior. The proper way is:

    //common.h:
    extern talker *pointerToTalker;    
    
    //common.cpp
    #include "common.h"
    talker* pointerToTalker = new talker;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm newer in JavaScript.So maybe my question will seem naive. My JavaScript Code: <script
Maybe I am just over thinking this and need to write some more prototype
I might be doing this wrong overall, but maybe someone will be able to
I'm newer in T-SQL.So maybe my question will seem naive. I need to retrieve
I've been trying this for hours, so maybe a fresh set of eyes will
maybe it's dumb but is there a difference between new Something().method(); and Something tmpSomething
Maybe its a very dumb question but I hope you can give me some
Maybe I'm missing it somewhere in the PHP manual, but what exactly is the
Maybe I should first give an idea of what I want to accomplish as
I have stumbled upon a riddle which I can't explain, maybe someone here will

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.