473,382 Members | 1,424 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

Global variable

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

May 15 '07 #1
6 2475
mi*************@litepoint.com wrote:
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?
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
May 15 '07 #2
mi*************@litepoint.com wrote:
What am I doing wrong?
you are not posting the code that doesn't work.

Regards,

Zeppe
May 15 '07 #3
<mi*************@litepoint.comwrote:
>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?
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*.
May 15 '07 #4
On May 15, 11:35 am, Zeppe
<zeppe@.remove.all.this.long.comment.email.itwrote :
mickey.marsh...@litepoint.com wrote:
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);
}

May 15 '07 #5
mi*************@litepoint.com wrote:
On May 15, 11:35 am, Zeppe
<zeppe@.remove.all.this.long.comment.email.itwrote :
>mickey.marsh...@litepoint.com wrote:
>>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.
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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
May 15 '07 #6
On Tue, 15 May 2007 15:08:08 -0400 in comp.lang.c++, "Victor Bazarov"
<v.********@comAcast.netwrote,
>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.

May 16 '07 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: David Hitillambeau | last post by:
Hi guys, As I am new to Python, i was wondering how to declare and use global variables. Suppose i have the following structure in the same module (same file): def foo: <instructions>...
4
by: Andrew V. Romero | last post by:
I have been working on a function which makes it easier for me to pull variables from the URL. So far I have: <script language="JavaScript"> var variablesInUrl; var vArray = new Array(); ...
4
by: Dan Elliott | last post by:
Hello, Converting from a working C program to C++, I run into the following error: I have a header: (header.h) namespace shared{ ... struct X{ ...
2
by: Thomas Matthews | last post by:
Hi, I'm getting linking errors when I declare a variable in the global scope, but not inside a function. The declarations are the same (only the names have been changed...). class Book {...
8
by: lawrence | last post by:
I'm learning Javascript. I downloaded a script for study. Please tell me how the variable "loop" can have scope in the first function when it is altered in the second function? It is not defined...
17
by: MLH | last post by:
A97 Topic: If there is a way to preserve the values assigned to global variables when an untrapped runtime error occurs? I don't think there is, but I thought I'd ask. During development, I'm...
10
by: Charles O'Flynn | last post by:
As a complete newcomer (2-3 days) to PHP, although not to programming in general, I have 'dived in' to start a small project to read and parse an XML data stream. I have already worked out most of...
9
by: Ed Jensen | last post by:
I'm having a vexing problem with global variables in Python. Please consider the following Python code: #! /usr/bin/env python def tiny(): bar = for tmp in foo: bar.append(tmp) foo = bar
1
weaknessforcats
by: weaknessforcats | last post by:
C++: The Case Against Global Variables Summary This article explores the negative ramifications of using global variables. The use of global variables is such a problem that C++ architects have...
112
by: istillshine | last post by:
When I control if I print messages, I usually use a global variable "int silent". When I set "-silent" flag in my command line parameters, I set silent = 1 in my main.c. I have many functions...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.