473,668 Members | 2,583 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

silly little question on variable scope

What's wrong with this:

//file1.h

Class A {
public:
A(){i=1;}
~A(){;}
void doThis(void){;}
void doThat(void){;}

private:
int i;
};
//file2.h
#include "file1.h"

A a;

Class B {
public:
B(){
//a = new A(); //will not compile
//a.doThis();
}
~B(){;}
};
Anyone know why I can't use a (type A) in class B ?

Mar 23 '06 #1
2 1697
Because you declared A as a concrete type instead of as a pointer.
You need a pointer to use new A()

A *a = new A;

But in general, you should avoid using pointers if you don't have to.
If you don't need to use the new operator to create an instance of an
object, than don't use it, and declare it as a concrete object instead.

Mar 23 '06 #2
Bit byte wrote:
What's wrong with this:

//file1.h

Class A {
public:
A(){i=1;}
~A(){;}
void doThis(void){;}
void doThat(void){;}

private:
int i;
};
//file2.h
#include "file1.h"

A a;

Class B {
public:
B(){
//a = new A(); //will not compile
//a.doThis();
}
~B(){;}
};
Anyone know why I can't use a (type A) in class B ?


You can. Your problems don't arise from that.

First, you've got a variable at file scope in an h file.
You don't usually want that. If you include file1.h in
more than one .cpp file, you get trouble. If you want
an instance of class A at file scope, you need to
declare it in the .cpp file where you want it, not in
a header file.

Next, as somebody pointed out, you've got a call
to new and you are putting the result into a non-pointer.
Calls to new return pointers to allocated memory.
Read up on pointers.

Next, you are doing something pretty whack even
if you do change the variable a to a pointer, and
even if you do have only one instance of a at
file scope. You are assigning a pointer to a "newed"
instance to the variable a. But the variable a is
outside the scope of the class B that is doing it.
So, the second time you do this, you will wind
up with a resource leak.

Probably what you want is something like so. First,
drop the variable a entirely. Then add a pointer as
a member of class B. Then add the appropriate
thing to the dtor of B to avoid the memory leak.

#include "file1.h"

Class B {
public:
B(){
pa = new A(); //will not compile
pa->doThis();
}
~B(){delete pa;}
private:
A * pa;
};

Socks

Mar 23 '06 #3

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

Similar topics

23
63666
by: stewart.midwinter | last post by:
No doubt I've overlooked something obvious, but here goes: Let's say I assign a value to a var, e.g.: myPlace = 'right here' myTime = 'right now' Now let's say I want to print out the two vars, along with their names. I could easily do this: print "myPlace = %s, myTime = %s" % (myPlace, myTime)
7
2662
by: YGeek | last post by:
Is there any difference between declaring a variable at the top of a method versus in the code of the method? Is there a performance impact for either choice? What about if the method will return before the variable is used? I'm trying to get an idea of whether the .NET compilers for VB.NET and C# will move all variable declaration to the beginning of a method or whether they will allocate the memory as it is needed by the method to...
3
2006
by: Grant Wagner | last post by:
Given the following working code: function attributes() { var attr1 = arguments || '_'; var attr2 = arguments || '_'; return ( function (el1, el2) { var value1 = el1 + el1; var value2 = el2 + el2; if (value1 > value2) return 1;
4
14888
by: Gery D. Dorazio | last post by:
Gurus, If a static variable is defined in a class what is the scope of the variable resolved to for it to remain 'static'? For instance, lets say I create a class library assembly that is strongly name which contains the class where the static variable is defined. This library can be referenced by multiple projects. I am fairly sure the static variable does not survive across the application boundry but does it within the application...
4
914
by: Jethro | last post by:
Hi all in my VB6 project, every function had a tidyup section at the end (approximates to the "finally" contsruct in a try..catch block). It did a SET OBJECT=NOTHING, for explicit memory release. In .NET, is this totally redundant, and I'm best off letting the compiler handle things ?
0
35215
MMcCarthy
by: MMcCarthy | last post by:
We often get questions on this site that refer to the scope of variables and where and how they are declared. This tutorial is intended to cover the basics of variable scope in VBA for MS Access. For the sake of brevity I am sticking to common usage. Wherever the term procedure is used in this tutorial it actually refers to a subroutine or function. Definition of Scope The scope of a variable where this variable can be seen or accessed...
2
4704
by: Shraddha | last post by:
Can we declare extern variable as static? What will be the scope of the variable then? What if we change the value of the variable in some other function? Also can someone tell me that if we can declare the global variable...and it is having scope throughout the file then what is so different in extern variable???
5
2229
by: somenath | last post by:
Hi All , I have one question regarding scope and lifetime of variable. #include <stdio.h> int main(int argc, char *argv) { int *intp = NULL; char *sptr = NULL;
3
2040
by: SRoubtsov | last post by:
Dear all, Do you know whether ANSI C (or some other dialects) support the following: * a variable name coincides with a type name, * a structure/union field name coincides with a type name in the same file (.c + all relevant .h's)? e.g.
0
8893
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8583
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8656
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7401
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6209
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5681
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4205
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4380
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2791
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.