Connecting Tech Pros Worldwide Help | Site Map

Global variable

mickey.marshall@litepoint.com
Guest
 
Posts: n/a
#1: May 15 '07
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.
Quote:
>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

Victor Bazarov
Guest
 
Posts: n/a
#2: May 15 '07

re: Global variable


mickey.marshall@litepoint.com wrote:
Quote:
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.
Quote:
>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?
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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask


Zeppe
Guest
 
Posts: n/a
#3: May 15 '07

re: Global variable


mickey.marshall@litepoint.com wrote:
Quote:
What am I doing wrong?
you are not posting the code that doesn't work.

Regards,

Zeppe
osmium
Guest
 
Posts: n/a
#4: May 15 '07

re: Global variable


<mickey.marshall@litepoint.comwrote:
Quote:
>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.
Quote:
>>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?
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*.


mickey.marshall@litepoint.com
Guest
 
Posts: n/a
#5: May 15 '07

re: Global variable


On May 15, 11:35 am, Zeppe
<zeppe@.remove.all.this.long.comment.email.itwrote :
Quote:
mickey.marsh...@litepoint.com wrote:
Quote:
What am I doing wrong?
>
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);
}

Victor Bazarov
Guest
 
Posts: n/a
#6: May 15 '07

re: Global variable


mickey.marshall@litepoint.com wrote:
Quote:
On May 15, 11:35 am, Zeppe
<zeppe@.remove.all.this.long.comment.email.itwrote :
Quote:
>mickey.marsh...@litepoint.com wrote:
Quote:
>>What am I doing wrong?
>>
>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
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.
Quote:
void InitAB();
#endif
>
globals.cpp
#include ".\globals.h"
Replace \ with /.

Add here:

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

(those are the definitions)
Quote:
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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask


David Harmon
Guest
 
Posts: n/a
#7: May 16 '07

re: Global variable


On Tue, 15 May 2007 15:08:08 -0400 in comp.lang.c++, "Victor Bazarov"
<v.Abazarov@comAcast.netwrote,
Quote:
Quote:
>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.
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.

Closed Thread