Rookie c++ question, but Ive spent the last 5 years doing Java, where
everytime I created an object I used new. In c++ I can create my objects
without and its confusing me just a little.
I have a class called polynomial. Its a nothing little class right now,
with just int variables, a basic container class. Im using it as I go
through some tutorials, but in this particular tutorial its telling me to do
polynomial *first = new polynomial();
but before I found this site I was just doing
polynomial first;
Im also struggling through pointers. I understand the basics, but fail to
see the advantage using them with my objects so quickly. Is one way better
than the other?
Thanks
Jul 22 '05
24 2863
"Sam" <ma*******@hotm ail.com> schrieb im Newsbeitrag
news:cn******** **@kermit.esat. net... "Gernot Frisch" <Me@Privacy.net > 写入消息新闻:2v *************@u ni-berlin.de... <snip> Now we get a problem. You can write the following Java codes: Polynomial createNew() { return new Polynomial(); } <snap>
Why does one want to do this:
Polynomial* p1 = OtherPoly.creat eNew();
instead of: Polynomial p1 = OtherPoly;
I didn't mean that createNew() is a method of class Polynomial. However, no matter whether it is or not. It's not my point. I was just saying that, you can easily create a new object in a function in Java and return its handle(or reference, or pointer) to the caller so that the caller can control that object(by calling its method). But in C++, you can't do this easily because the object you create within a function as a local variable is destroyed automatically after the function returns. Thus the pointer it returns doesn't point to a valid object any longer. So you have to use the new expression if you want an object to exist after the function that creates it returns. For example, suppose you have another class called MySystem, you do this in Java: MySystem sys = new MySystem(); Polynomial poly = sys.createNew() ; It works if you have the method createNew() in class MySystem: Polynomial createNew() { return new Polynomial(); }
In C++ I'd write:
MySystem sys;
Polynomial poly; sys.CreateNew(p oly); // Just set initial values
See - no new/delete required.
Avoid it where you can. It's more time efficient and if you come from
Java you don't get into memory leak trouble.
Just my .02$,
-Gernot
"Sam" <ma*******@hotm ail.com> wrote in message
news:cn******** **@kermit.esat. net... "Gernot Frisch" <Me@Privacy.net > 写入消息新闻:2v *************@u ni-berlin.de... <snip> Now we get a problem. You can write the following Java codes: Polynomial createNew() { return new Polynomial(); } <snap>
Why does one want to do this:
Polynomial* p1 = OtherPoly.creat eNew();
instead of: Polynomial p1 = OtherPoly;
I didn't mean that createNew() is a method of class Polynomial. However, no matter whether it is or not. It's not my point. I was just saying that, you can easily create a new object in a function in Java and return its handle(or reference, or pointer) to the caller so that the caller can control that object(by calling its method). But in C++, you can't do this easily because the object you create within a function as a local variable is destroyed automatically after the function returns. Thus the pointer it returns doesn't point to a valid object any longer. So you have to use the new expression if you want an object to exist after the function that creates it returns. For example, suppose you have another class called MySystem, you do this in Java: MySystem sys = new MySystem(); Polynomial poly = sys.createNew() ; It works if you have the method createNew() in class MySystem: Polynomial createNew() { return new Polynomial(); }
But in C++, if you write: MySystem sys; Polynomial* p = sys.createNew() ;
you must have the mothod createNew() in class MySystem as follows Polynomial* MySystem::creat eNew() { return new Polynomial; }
rather than Polynomial* MySystem::creat eNew() { Polynomial po; return &po; // oops! a bad-point-to-be }
But you can simply write
Polynomial MySystem::creat eNew()
{
Polynomial po;
return po;
}
There is no need for pointers with all the trouble that they bring. If you
are worried about the cost of copying a Polynomial object then the answer is
to make that class efficiently copyable (by using smart pointers for
instance) not to avoid copying by introducing pointers.
john
> In C++ I'd write: MySystem sys; Polynomial poly; sys.CreateNew(p oly); // Just set initial values
It's just an example showing some differences between Java and C++. Actually
You cannot always avoid pointers in C++. Polynomial may be an abstract base
class here if I write a createNew() method for it. If I wanna just set
initial values, I should call it initPoly() something. And it is probable
that I wanna keep holding that new object after the function calling
createNew() returns. See - no new/delete required. Avoid it where you can. It's more time efficient and if you come from Java you don't get into memory leak trouble.
Just my .02$, -Gernot
> But you can simply write Polynomial MySystem::creat eNew() { Polynomial po; return po; }
There is no need for pointers with all the trouble that they bring. If you are worried about the cost of copying a Polynomial object then the answer is to make that class efficiently copyable (by using smart pointers for instance) not to avoid copying by introducing pointers.
john
Yes. You're right. But may I argue that you need pointers for polymorphism
at least. "References " in c++ are more confused than pointers, I think.
"John Harrison" <jo************ *@hotmail.com> wrote in message
news:2v******** *****@uni-berlin.de... "Rv5" <rm*****@adelph ia.net> wrote in message news:6a******** ************@ad elphia.com... Rookie c++ question, but Ive spent the last 5 years doing Java, where everytime I created an object I used new. In c++ I can create my objects without and its confusing me just a little.
I have a class called polynomial. Its a nothing little class right now, with just int variables, a basic container class. Im using it as I go through some tutorials, but in this particular tutorial its telling me to do polynomial *first = new polynomial(); but before I found this site I was just doing polynomial first;
It's likely that you are right and the tutorial is wrong.
Hello...? Did it occur to you that the purpose of that particular tutorial
might be to *teach* using pointers? One does not learn how to use pointers
by avoiding them.
Pointers are useful in many situations, but they are most often useful when
storing polymorphic objects in containers. That is, storing pointers to
base class objects in a vector (for example), when those pointers actually
point to dynamically created instances of derived class objects. Of course,
that's something you wouldn't tackle until later, if you're just starting on
pointers, but it's an example of where they are commonly used.
All that said, I do agree that if you don't actually *need* pointers, don't
bother using them. You'll find out soon enough if there's a case where you
*do* need a pointer. (And just because a function you're calling requires a
pointer as a parameter, doesn't mean that you have to pass it a pointer
variable...you more likely want to pass the address of an existing object.)
At some point, you'll also want to look at using "smart" pointers. They
really help make code better in those cases where you *do* need pointers.
-Howard
> At some point, you'll also want to look at using "smart" pointers. They really help make code better in those cases where you *do* need pointers.
....depending on what you call "better" code. But, I agree, there's
situations where smarties are more appropreate than simple pointers...
-Gernot
Thanks for all the info guys. Lot to absorb. Im going to have to read each
post carefully, maybe ever a couple times to really grab what is going on
here. C++ is definitely a lot more indepth then java is.
"Sam" <ma*******@hotm ail.com> wrote in message
news:cn******** **@kermit.esat. net... But you can simply write
Polynomial MySystem::creat eNew() { Polynomial po; return po; }
There is no need for pointers with all the trouble that they bring. If you are worried about the cost of copying a Polynomial object then the answer is to make that class efficiently copyable (by using smart pointers for instance) not to avoid copying by introducing pointers.
john Yes. You're right. But may I argue that you need pointers for polymorphism at least. "References " in c++ are more confused than pointers, I think.
Rv5 wrote: Thanks for all the info guys. Lot to absorb. Im going to have to read each post carefully, maybe ever a couple times to really grab what is going on here. C++ is definitely a lot more indepth then java is.
Honestly. Not a good idea.
A better idea is to get a book. There are so many pitfalls and
things to know in C++, that you can't discover them on your own
without major frustration.
--
Karl Heinz Buchegger kb******@gascad .at
> Only use a pointer when you need to control the lifetime of an object.
What if the type of object to be created is not known until run-time?
What if the number of objects to be created is not known until run-time?
At the lowest level I believe the new operator would be required.
Of course at a higher level one could use abstractions to avoid having to
explicitly use new and delete.
"DaKoadMunk y" <da*********@ao l.com> wrote in message
news:20******** *************** ****@mb-m11.aol.com... Only use a pointer when you need to control the lifetime of an object.
What if the type of object to be created is not known until run-time?
Such an object has it's lifetime managed by the programmer. What's your
point? This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Yi |
last post by:
Hi,
I am a scientist and new to .NET programming. On my PC, I am using Windows
2000 professional with IIS, SQL Server 2000 (standard, personal), Visual
Basic .net (2003, standard). By following the demo code, I can generate the
web form to access the Northwind data in SQL server. When I tried to use
Server Explorer to add new Data Connection to Northwind, the 鈥渢est
connection鈥 always work, but click OK always give an error message:...
|
by: dpr |
last post by:
I have come accross a piece of C++ code with the construct:
MyClass *c = new class MyClass();
Is there a difference between this and:
MyClass *c = new MyClass();
?
|
by: Leslaw Bieniasz |
last post by:
Cracow, 28.10.2004
Hello,
I have a program that intensively allocates and deletes lots
of relatively small objects, using "new" operator. The objects
themselves are composed of smaller objects, again allocated using "new".
From my tests I deduce that a considerable part of the computational
time is spent on the memory allocation, which makes the program
substantially slower compared to the equivalent code written using
|
by: Ben R. |
last post by:
I'm curious about the differeng behavior of the "new" keyword when dealing
with value versus object types. If I'm correct, when I do:
dim x as integer
There's no need for "new" to be called since this "value" type is all set to
go. Is this because value types are stored on the stack so the memory
initialization is already taken care of?
Now, when dealing with an object type, it seems you do need to use the "new"
|
by: Ron Cook |
last post by:
So I'll type something like:
Dim cmd As New SqlCommand()(
But for some things, it doesn't like "As New" and wants me to type like:
Dim nod As employeeNode
When do I know when to use "As New" as opposed to not? Intellisense always
sets me straight, but I'd like to understand better.
| |
by: Medvedev |
last post by:
i see serveral source codes , and i found they almost only use "new"
and "delete" keywords to make they object.
Why should i do that , and as i know the object is going to be destroy
by itself at the end of the app
for example:
class test
{
public:
int x;
|
by: jonceramic |
last post by:
Hi All,
My apologies for asking something that I'm sure has been answered
for. But, my google searching can't find a proper set of keywords.
I would like to add "New..." or "other..." or "Show All" to my combo
boxes.
I'm tired of having to put buttons beside them for "special options".
|
by: Jordi |
last post by:
I'm getting the following error:
Software error:
Can't locate object method "new" via package "A::B" at /path/file.cgi line 5.
My code is basically this:
#!/usr/bin/perl -w
use strict;
use warnings;
use A::B;
my $test = new A::B;
|
by: tvnaidu |
last post by:
I compiled tinyxml files (.cpp files) and when I am linking to create final exe in Linux, I am getting lot of errors saying "undefiend reference" to "operator new" and "delete", any idea?.
Main.cpp:377: undefined reference to `operator delete(void*)'
tinyXML/include/tinystr.h:259: undefined reference to `operator delete(void*)'
../common/tinyXML/include/tinyxml.h:1401: undefined reference to `operator delete(void*)'...
|
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
| |
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梡lanning, coding, testing, and deployment梬ithout 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...
|
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...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |