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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T20:56:47+00:00 2026-05-24T20:56:47+00:00

I have two ‘simple’ questions in C++. 1- I want to make a variable

  • 0

I have two ‘simple’ questions in C++.

1- I want to make a variable shared by some methods in a class, so what i am doing is declaring the variable in the header file and make it global to the class. Hence, all methods will have access to it. Is this the right way of doing it ?

2- Following my first point, if i declare variable N as double *N in the header. Then inside one of my methods, i do

N = (double*) malloc (sizeof(double)*50);

I know that N will have a memory allocation inside the function. My question is: is this memory reserved only inside the function or it stays for the global N all the time ?

If the memory won’t be reserved for N, then this method wouldn’t be a good method, as the system might overwrite the values of N later during the program.

Please kindly advice, Thank you

EDIT: Thank you all for the input and for editing my question. Apparently i mistakenly called my variable as global while it is actually a class member. I thought this declaration is called global since the variable is global for the methods inside the class. Hope i ask a ‘smarter’ question next time 🙂

  • 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-24T20:56:49+00:00Added an answer on May 24, 2026 at 8:56 pm

    Let’s clear things up a bit. Assuming this is in header members.h:

    extern double a;     // this is a "global" variable declaration. It needs a definition.
    
    class B {
      double c;          // this is a data member. There will be one in each object of class B
      static double * d; // this is a static data member. There will be a single one for
                         // all objects of class B. It needs a definition.
    public:
      void f() { c = 1.0; a = 3.0; }           // A member function can access both static and
                                               // plain data members, as well as globals.
      static void g() { d = new double(2.0); } // A static member function can only access
                                               // static data members and globals.
    };
    

    Note that variables/data members like a or c do not require explicit allocation. On the other hand d does, because it is a pointer. Note also that in C++ you use new() rather than malloc().

    This is source file members.cpp:

    #include "members.h"
    
    double a; // Definition of global a
    
    double * B::d;  // Definition for static member d
    
    B b1;     // Static instance of class B
    
    int main() {
      B b2;      // Local instance of class B
      b1.f();    // Calling member function f() on b1
      b2.f();    // Calling member function f() on b2
      B::g();    // Calling static member function g()
    }
    

    I hope I’ve been clear enough and covered your doubts.

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

Sidebar

Related Questions

I have two classes, and want to include a static instance of one class
I have two classes Order and Items I want a method like this class
Have two UIBarButtonItems want to make it as one UIBarButtonItem and toggle between them
I have two arrays of System.Data.DataRow objects which I want to compare. The rows
I have two spreadsheets... when one gets modified in a certain way I want
I have two webapplication, one is a simple authenticationsite which can authenticate the logged
I have two inputs with the same date, but i want to separate the
I have two questions, and should mention I am an Java programmer going into
I have two different Classes: class X { public int TX1 { get; set;
I have two tables, say teacher and student. I want to build a third

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.