Global variable

M

mickey.marshall

I have a simlpe project that has a base class and two inherited
classes.
I want to make a variable that gets initialzed and set one time and is
then accessable from all inherited classes.
I therefore made a global variable list in global.h and a function to
initialize them (pointers) in global.cpp.
From the main dialog class I call the global init sub and then I call
the master class to set the values. The I call the inherited class to
act on the variables but in the inherited class, the variables are not
initialized.
What am I doing wrong?

MickeyM
 
V

Victor Bazarov

I have a simlpe project that has a base class and two inherited
classes.
I want to make a variable that gets initialzed and set one time and is
then accessable from all inherited classes.
I therefore made a global variable list in global.h and a function to
initialize them (pointers) in global.cpp.
the master class to set the values. The I call the inherited class to
act on the variables but in the inherited class, the variables are not
initialized.
What am I doing wrong?

You're not reading the FAQ before posting. See FAQ 5.8. Hint: you've
made a mistake on the line 42 of your code.

V
 
O

osmium

I have a simlpe project that has a base class and two inherited
classes.
I want to make a variable that gets initialzed and set one time and is
then accessable from all inherited classes.
I therefore made a global variable list in global.h and a function to
initialize them (pointers) in global.cpp.
the master class to set the values. The I call the inherited class to
act on the variables but in the inherited class, the variables are not
initialized.
What am I doing wrong?

Too many words and too little code. You should be able to throw something
together that illustrates your problem in ten or 15 minutes. Post *that*.
 
M

mickey.marshall

you are not posting the code that doesn't work.

Regards,

Zeppe

Here is the sample code:
Main Code
#include "stdafx.h"
#include "test.h"
#include "testDlg.h"
#include ".\testdlg.h"
#include ".\arithmetic.h"

..
..
..
void CtestDlg::OnBnClickedButton1()
{
// TODO: Add your control notification handler code here
int rv;
InitAB();
CAddTwoNumbers Add2;
//CSubtract Sub2;
Arithmetic *ar1=&Add2;
//Arithmetic *ar2=&Sub2;
//ar= new Arithmetic;
ar1->EnterTwoNumbers(2,4);
//ar2->EnterTwoNumbers(8,4);
rv=Add2.Add ();
//rv=Sub2.Sub2 ();

}

Globals.h
#ifndef MYGLOBALS_H
#define MYGLOBALS_H
static int *a1; //this is the global variable
static int *b1; //this is the global variable
void InitAB();
#endif

globals.cpp
#include ".\globals.h"
void InitAB()
{
a1=new int;
b1=new int;
}

Master class (arithmetic.cpp)
#include "StdAfx.h"
#include ".\arithmetic.h"
//#include ".\globals.h"
//extern int a1,b1;
Arithmetic::Arithmetic(void)
{
}

Arithmetic::~Arithmetic(void)
{
}
void Arithmetic::EnterTwoNumbers(int a, int b)// this works fine
{
*a1=a;
*b1=b;

}
void Arithmetic::GetTwoNumbers(int * a, int * b)
{
if (NULL!=a)
if (NULL!=b)
{
*a=*a1;
*b=*b1;
}
}

inherited class

#include "StdAfx.h"
#include ".\arithmetic.h"
//#include ".\globals.h"
//extern int a1,b1;

CAddTwoNumbers::CAddTwoNumbers(void)
{
}

CAddTwoNumbers::~CAddTwoNumbers(void)
{
}
CAddTwoNumbers::Add(void)// a1 and b1 are both NULL here
{
return(*a1+*b1);
}
 
V

Victor Bazarov

Here is the sample code:
Main Code
#include "stdafx.h"
#include "test.h"
#include "testDlg.h"
#include ".\testdlg.h"
#include ".\arithmetic.h"

.
.
.
void CtestDlg::OnBnClickedButton1()
{
// TODO: Add your control notification handler code here
int rv;
InitAB();
CAddTwoNumbers Add2;
//CSubtract Sub2;
Arithmetic *ar1=&Add2;
//Arithmetic *ar2=&Sub2;
//ar= new Arithmetic;
ar1->EnterTwoNumbers(2,4);
//ar2->EnterTwoNumbers(8,4);
rv=Add2.Add ();
//rv=Sub2.Sub2 ();

}

Globals.h
#ifndef MYGLOBALS_H
#define MYGLOBALS_H
static int *a1; //this is the global variable
static int *b1; //this is the global variable

The comments are incorrect. Those are not global variables.
Those are file-scoped variables living outside of any function.
The main thing here is to understand that there is a copy of
each in each tranlsation unit.

Replace the word 'static' with the word 'extern', and you get
closer to the truth. You will still need to _define_ those in
some (only one) translation unit before attempting to use them.
I would put the definitions in the same file where 'InitAB' is.
void InitAB();
#endif

globals.cpp
#include ".\globals.h"

Replace \ with /.

Add here:

int *a1 = 0;
int *b1 = 0;

(those are the definitions)
void InitAB()
{
a1=new int;
b1=new int;
}

Master class (arithmetic.cpp)
#include "StdAfx.h"
#include ".\arithmetic.h"
//#include ".\globals.h"
//extern int a1,b1;
Arithmetic::Arithmetic(void)
{
}

Arithmetic::~Arithmetic(void)
{
}
void Arithmetic::EnterTwoNumbers(int a, int b)// this works fine
{
*a1=a;
*b1=b;

}
void Arithmetic::GetTwoNumbers(int * a, int * b)
{
if (NULL!=a)
if (NULL!=b)
{
*a=*a1;
*b=*b1;
}
}

inherited class

#include "StdAfx.h"
#include ".\arithmetic.h"
//#include ".\globals.h"
//extern int a1,b1;

CAddTwoNumbers::CAddTwoNumbers(void)
{
}

CAddTwoNumbers::~CAddTwoNumbers(void)
{
}
CAddTwoNumbers::Add(void)// a1 and b1 are both NULL here
{
return(*a1+*b1);
}

V
 
D

David Harmon

On Tue, 15 May 2007 15:08:08 -0400 in comp.lang.c++, "Victor Bazarov"
The comments are incorrect. Those are not global variables.
Those are file-scoped variables living outside of any function.
The main thing here is to understand that there is a copy of
each in each tranlsation unit.

Replace the word 'static' with the word 'extern', and you get
closer to the truth. You will still need to _define_ those in
some (only one) translation unit before attempting to use them.
I would put the definitions in the same file where 'InitAB' is.

More confusing still, according to Mickey's original description they
were supposed to have belonged to the base class, or in c++ terms they
should be static member variables. But a different "static".

And of course there is _nothing_ in the posted code that remotely
justifies making them pointers or using new, which is a very bad thing
to do without a reason. I can only assume that in the real application
there is some reason for it.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,066
Latest member
VytoKetoReviews

Latest Threads

Top