473,399 Members | 2,858 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,399 software developers and data experts.

Creating an instance of a class

Dear all,

I am more or less new to c++ programing and therefore still have
problems with some fundamentals :(

At the moment I try to build a GUI-Application with Qt4. Sometimes I
have seen in the tutorials I have to instance a class with the new
command. Like QWidget QLabel *label = new QLabel("Hello World"); Then
I have created a pointer to a QLabel Object. The other way I have seen
is to just use QLabel label("Hello World"); Without a pointer and
without the new statement.

So to access the functions of the QLabel class I have to use the "."
for the part without pointer like label.show and for the pointer part:
label->show.

Can somebody explain me, why it is possible to use the two ways and
what is the difference in general.

Thank you a lot in advance.

jimbo

Feb 19 '07 #1
6 1958
On Feb 19, 9:16 am, "jimbo" <joachim.zett...@googlemail.comwrote:
Dear all,

I am more or less new to c++ programing and therefore still have
problems with some fundamentals :(

At the moment I try to build a GUI-Application with Qt4. Sometimes I
have seen in the tutorials I have to instance a class with the new
command. Like QWidget QLabel *label = new QLabel("Hello World"); Then
I have created a pointer to a QLabel Object. The other way I have seen
is to just use QLabel label("Hello World"); Without a pointer and
without the new statement.

So to access the functions of the QLabel class I have to use the "."
for the part without pointer like label.show and for the pointer part:
label->show.

Can somebody explain me, why it is possible to use the two ways and
what is the difference in general.
You really should get yourself a good book if you haven't got one
already because it can take some time to learn those things and there
are a lot of things to keep in mind which I can't explain in one post
(but you are always welcome to ask questions).

The difference between using 'QLabel label;' and QLabel* label = new
QLabel();' is where the instance is stored and its scope. When not
using new the instance will be created on the stack and will be
destructed when it goes out of scope (this usually coincide with a
'}').

When using new the object is created on the heap and will remain there
until you call delete on a pointer pointing to it. This means that an
object instantiated in one function using new can be used in another
function (even if the first function has returned) if given a pointer
to it.

For every time you create an object with new you must (for each and
every path of execution) call delete on a pointer to that object or
your object will leak memory. To make things a bit more trickier this
is not always true when using Qt since a parent object will usually
delete/destruct all child objects on destruction.

An advice regarding dynamically allocated memory (using new) is to not
use it unless you have to.

--
Erik Wikström

Feb 19 '07 #2
jimbo wrote:
Dear all,

I am more or less new to c++ programing and therefore still have
problems with some fundamentals :(

At the moment I try to build a GUI-Application with Qt4. Sometimes I
have seen in the tutorials I have to instance a class with the new
command. Like QWidget QLabel *label = new QLabel("Hello World"); Then
I have created a pointer to a QLabel Object. The other way I have seen
is to just use QLabel label("Hello World"); Without a pointer and
without the new statement.

So to access the functions of the QLabel class I have to use the "."
for the part without pointer like label.show and for the pointer part:
label->show.

Can somebody explain me, why it is possible to use the two ways and
what is the difference in general.

Thank you a lot in advance.

jimbo
Given an object and a pointer to that object, you can access members
like so:

class foo
{
public:
int bar;
};

int
main()
{
foo* baz1 = new foo();
foo baz2;

baz2.bar; // cool
baz2->bar; // not cool
baz1.bar; // not cool
(*baz1).bar; // cool but too long
(baz1)->bar; // short hand version of the previous line
baz1->bar; // even shorter version of the previous line
}

In summary, "->" is a short hand notation to dereference the
pointer first and then access its member.

Good Luck!
Feb 19 '07 #3
On 19 Feb., 09:55, Piyo <cybermax...@yahoo.comwrote:
jimbo wrote:
Dear all,
I am more or less new to c++ programing and therefore still have
problems with some fundamentals :(
At the moment I try to build a GUI-Application with Qt4. Sometimes I
have seen in the tutorials I have to instance a class with the new
command. Like QWidget QLabel *label = new QLabel("Hello World"); Then
I have created a pointer to a QLabel Object. The other way I have seen
is to just use QLabel label("Hello World"); Without a pointer and
without the new statement.
So to access the functions of the QLabel class I have to use the "."
for the part without pointer like label.show and for the pointer part:
label->show.
Can somebody explain me, why it is possible to use the two ways and
what is the difference in general.
Thank you a lot in advance.
jimbo

Given an object and a pointer to that object, you can access members
like so:

class foo
{
public:
int bar;

};

int
main()
{
foo* baz1 = new foo();
foo baz2;

baz2.bar; // cool
baz2->bar; // not cool
baz1.bar; // not cool
(*baz1).bar; // cool but too long
(baz1)->bar; // short hand version of the previous line
baz1->bar; // even shorter version of the previous line

}

In summary, "->" is a short hand notation to dereference the
pointer first and then access its member.

Good Luck!

Hi to all of you,

first of all, thanks a lot for the explanation. I didn´t know that
this whole topic is related to where the memory is allocated. Now with
heap and stack I can imagine a little bit better what I am doing :)

But to come back to my own little example. When i am already in a
main() function of my app and i create the instances there without the
new operator then i will have no problems concerning the use of the
instance in a different scope because i am already in main and main is
not ended yet, or?

I think i need to take it more carefully when i implement an own class
and use functions there.

Thx for your help.

Best regards,

jimbo

Feb 19 '07 #4
jimbo wrote:
But to come back to my own little example. When i am already in a
main() function of my app and i create the instances there without the
new operator then i will have no problems concerning the use of the
instance in a different scope because i am already in main and main is
not ended yet, or?
After main ends the destructors of "static" storage objects will run. Make
sure that none of those will reference objects from main's stack.
I think i need to take it more carefully when i implement an own class
and use functions there.
Yeah, I think too many examples of Qt's documentation use dynamic memory
allocation when they could use "auto" memory allocation just fine. Not to
mention they have some strange system of keeping track of dynamic memory
objects themselves (if derived from QObject I think) which tends to lead to
a coding style where one forgets to use delete (it may be OK with QObject
derived objects but certainly it's not a good coding style to apply outside
of Qt).

--
Dizzy
http://dizzy.roedu.net

Feb 19 '07 #5
On Feb 19, 10:06 am, "jimbo" <joachim.zett...@googlemail.comwrote:
On 19 Feb., 09:55, Piyo <cybermax...@yahoo.comwrote:
jimbo wrote:
Dear all,
I am more or less new to c++ programing and therefore still have
problems with some fundamentals :(
At the moment I try to build a GUI-Application with Qt4. Sometimes I
have seen in the tutorials I have to instance a class with the new
command. Like QWidget QLabel *label = new QLabel("Hello World"); Then
I have created a pointer to a QLabel Object. The other way I have seen
is to just use QLabel label("Hello World"); Without a pointer and
without the new statement.
So to access the functions of the QLabel class I have to use the "."
for the part without pointer like label.show and for the pointer part:
label->show.
Can somebody explain me, why it is possible to use the two ways and
what is the difference in general.
Thank you a lot in advance.
jimbo
Given an object and a pointer to that object, you can access members
like so:
class foo
{
public:
int bar;
};
int
main()
{
foo* baz1 = new foo();
foo baz2;
baz2.bar; // cool
baz2->bar; // not cool
baz1.bar; // not cool
(*baz1).bar; // cool but too long
(baz1)->bar; // short hand version of the previous line
baz1->bar; // even shorter version of the previous line
}
In summary, "->" is a short hand notation to dereference the
pointer first and then access its member.
Good Luck!

Hi to all of you,

first of all, thanks a lot for the explanation. I didn´t know that
this whole topic is related to where the memory is allocated. Now with
heap and stack I can imagine a little bit better what I am doing :)

But to come back to my own little example. When i am already in a
main() function of my app and i create the instances there without the
new operator then i will have no problems concerning the use of the
instance in a different scope because i am already in main and main is
not ended yet, or?
Yes, the scope of main is a superset of almost all other scopes
(static being the only exception that I can think of right now).
I think i need to take it more carefully when i implement an own class
and use functions there.
It's not so much when implementing a class as when using it. But since
we are on the topic of implementing classes and dynamic memory
allocation I can say that if you implementation uses new instantiate
members (perhaps in the constructor) you should probably delete them
in the destructor, like so:

class Foo {
int* arr; // An array
public:
Foo(int i) : arr(new int[i] { } // * allocate an array of size i
~Foo() { delete[] arr; } // Free the memory used
};

This way a user of the class will never have to worry about how the
class manages its resources, it will just work.

* If you don't understand the ': arr(new int[i] { }'-bit it's an
initializer-list and does the same thing as '{ arr = new int[i]; }',
for a more detailed explanation see http://www.parashift.com/c++-faq-lit....html#faq-10.6

--
Erik Wikström

Feb 19 '07 #6
On 19 Feb., 10:46, "Erik Wikström" <eri...@student.chalmers.sewrote:
On Feb 19, 10:06 am, "jimbo" <joachim.zett...@googlemail.comwrote:
On 19 Feb., 09:55, Piyo <cybermax...@yahoo.comwrote:
jimbo wrote:
Dear all,
I am more or less new to c++ programing and therefore still have
problems with some fundamentals :(
At the moment I try to build a GUI-Application with Qt4. Sometimes I
have seen in the tutorials I have to instance a class with the new
command. Like QWidget QLabel *label = new QLabel("Hello World"); Then
I have created a pointer to a QLabel Object. The other way I have seen
is to just use QLabel label("Hello World"); Without a pointer and
without the new statement.
So to access the functions of the QLabel class I have to use the "."
for the part without pointer like label.show and for the pointer part:
label->show.
Can somebody explain me, why it is possible to use the two ways and
what is the difference in general.
Thank you a lot in advance.
jimbo
Given an object and a pointer to that object, you can access members
like so:
class foo
{
public:
int bar;
};
int
main()
{
foo* baz1 = new foo();
foo baz2;
baz2.bar; // cool
baz2->bar; // not cool
baz1.bar; // not cool
(*baz1).bar; // cool but too long
(baz1)->bar; // short hand version of the previous line
baz1->bar; // even shorter version of the previous line
}
In summary, "->" is a short hand notation to dereference the
pointer first and then access its member.
Good Luck!
Hi to all of you,
first of all, thanks a lot for the explanation. I didn´t know that
this whole topic is related to where the memory is allocated. Now with
heap and stack I can imagine a little bit better what I am doing :)
But to come back to my own little example. When i am already in a
main() function of my app and i create the instances there without the
new operator then i will have no problems concerning the use of the
instance in a different scope because i am already in main and main is
not ended yet, or?

Yes, the scope of main is a superset of almost all other scopes
(static being the only exception that I can think of right now).
I think i need to take it more carefully when i implement an own class
and use functions there.

It's not so much when implementing a class as when using it. But since
we are on the topic of implementing classes and dynamic memory
allocation I can say that if you implementation uses new instantiate
members (perhaps in the constructor) you should probably delete them
in the destructor, like so:

class Foo {
int* arr; // An array
public:
Foo(int i) : arr(new int[i] { } // * allocate an array of size i
~Foo() { delete[] arr; } // Free the memory used

};

This way a user of the class will never have to worry about how the
class manages its resources, it will just work.

* If you don't understand the ': arr(new int[i] { }'-bit it's an
initializer-list and does the same thing as '{ arr = new int[i]; }',
for a more detailed explanation seehttp://www.parashift.com/c++-faq-lite/ctors.html#faq-10.6

--
Erik Wikström
Thx for the explanation. I added your link to my favourites...so I
will look there the next time before I post :)

Have a nice day.

Best regards,

jimbo

Feb 19 '07 #7

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

Similar topics

4
by: Edvard Majakari | last post by:
Greetings, fellow Pythonistas! I'm about to create three modules. As an avid TDD fan I'd like to create typical 'use-cases' for each of these modules. One of them is rather large, and I wondered...
2
by: jm | last post by:
I have a Form1 and two class files. In the first class, I call the second class. The second class references the Form1 notifyicon and changes the icon. It works. The problems is, however,...
6
by: Davinci_Jeremie | last post by:
Hi Newbee here to C# I have a simple questions... In a Hello world example how does the class object Hello exist with out creating it? I come from object pascal where everything object is...
15
by: Carlos Lozano | last post by:
Hi, What is the right way to create an OCX COM component. The component is already registerred, but can't create an instance. I am using the reference to the interop module created. If I use...
12
by: Mats Lycken | last post by:
Hi, I'm creating a CMS that I would like to be plug-in based with different plugins handling different kinds of content. What I really want is to be able to load/unload plugins on the fly without...
7
by: Brett Romero | last post by:
I need a static version of a class that can be referenced anywhere as a singleton and the same class that can be used as instances. Can this be done without basically creating the same class twice...
17
by: Lee Harr | last post by:
I understand how to create a property like this: class RC(object): def _set_pwm(self, v): self._pwm01 = v % 256 def _get_pwm(self): return self._pwm01 pwm01 = property(_get_pwm, _set_pwm)
26
by: nyathancha | last post by:
Hi, How Do I create an instance of a derived class from an instance of a base class, essentially wrapping up an existing base class with some additional functionality. The reason I need this is...
19
by: =?Utf-8?B?WWFua2VlIEltcGVyaWFsaXN0IERvZw==?= | last post by:
I'm doing my c# more and more like i used to code c++, meaning i'm casting more often than creating an instance of objects. like : protected void gvOrderDetailsRowDataBound(object sender,...
3
by: Peter Morris | last post by:
Hi all Let's say I am creating a model to represent classes and properties. In addition to this I need instances of classes and values for those properties. KEY: (AssociationEndName)
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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
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...
0
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,...
0
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...

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.