473,386 Members | 1,705 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,386 software developers and data experts.

can I declare a class variable inside that class method?

Hi,
Suppose I have a class,

class MyClass{
public:
MyClass();
MyClass(MyClass);

int func();
};

Then can I declare a MyClass object inside MyClass::func()?

int MyClass::func(){
MyClass obj;
<......>
}

Jul 22 '05 #1
2 1871
John Black wrote:

Suppose I have a class,

class MyClass{
public:
MyClass();
MyClass(MyClass);
Suppose you wrote

MyClass x;
MyClass y(x);

Because the copy constructor parameter is passed by value, a copy of x
is made, and the copy is sent to the copy constructor. But to make the
copy, the MyClass copy constructor would have to be invoked. And then
the argument would have to be copied, using the MyClass copy constructor
again. And so on...

The point is, your copy constructor should take a reference, not a copy:

MyClass(MyClass const &);
int func();
};

Then can I declare a MyClass object inside MyClass::func()?

int MyClass::func(){
MyClass obj;
<......>
}


Sure.

--
Russell Hanneken
eu*******@cbobk.pbz
Use ROT13 to decode my email address.
Jul 22 '05 #2

"John Black" <bl***@eed.com> wrote in message
news:40***************@eed.com...
Hi,
Suppose I have a class,

class MyClass{
public:
MyClass();
MyClass(MyClass);

int func();
};

Then can I declare a MyClass object inside MyClass::func()?

int MyClass::func(){
MyClass obj;
<......>
}


Yes. Just don't do this :-)
int MyClass::func()
{
MyClass obj;
obj.func();
}
Jul 22 '05 #3

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

Similar topics

6
by: rick | last post by:
Noob problem. I prefer to keep all my scripts in an external '.js' file. I am currently loading the external '.js' file from the header. Problem is I would like to declare a global variable in the...
4
by: Mark | last post by:
Hi, is it possible to declare a delegate inside a routine? E.g private void MySub() { delegate int oAddMe(int n1, int n2); } Or can delegates only be declared with a global scope inside...
15
by: Geoff Cox | last post by:
Hello, Can I separately declare and initialize a string array? How and where would I do it in the code below? It was created using Visual C++ 2005 Express Beta 2 ... In C# I would have ...
6
by: The8thSense | last post by:
how to declare static varible and static method inside a class ? i try "public static ABC as integer = 10" and it said my declaration is invalid Thanks
2
by: Jul | last post by:
Hi, Maybe configuration for getting the same result can be simpler but I will describe my real one. I have one solitions which contains 2 projects: Class Lib and WinApp. Class Lib define...
7
by: WXS | last post by:
Vote for this idea if you like it here: http://lab.msdn.microsoft.com/productfeedback/viewfeedback.aspx?feedbackid=5fee280d-085e-4fe2-af35-254fbbe96ee9...
16
by: digitalorganics | last post by:
What's the difference between initializing class variables within the class definition directly versus initializing them within the class's __init__ method? Is there a reason, perhaps in certain...
3
by: johnmmcparland | last post by:
Hi all, I would like to have a static constant array inside a class definition which would contain the number of days in each month (I am writing a Date class as an exercise). However my...
20
by: tshad | last post by:
Using VS 2003, I am trying to take a class that I created to create new variable types to handle nulls and track changes to standard variable types. This is for use with database variables. This...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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...
0
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.