473,587 Members | 2,448 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

is it a good idea to assign object to pointer without initialising it to NULL.

hi all,
is it a good idea to assign object to pointers without initialising it
to NULL?
suppose if i have a class TEST

class ELEMENT
{
private:
int a;
string s1;
public:
func(a);
AddChild()
{
cout<<"some value";
}
};

i have object of TEST as shown below:
ELEMENT *inventors=bibl io->AddChild("Invs ");
ELEMENT *t2;
is it better to initialize while declaring itself?
i mean,if i say :

ELEMENT *t2= inventors->AddChild("Inv" );// is this safe enough?

or do i need to go with usual conventions like:
ELEMENT *t2=NULL;
t2= inventors->AddChild("Inv" );

so please help me which one is better and safe?

if not please explain me what could go wrong with

ELEMENT *t2= inventors->AddChild("Inv" );

thanks & regards
Nagaraj Hayyal

Sep 19 '05 #1
8 1768
nrhayyal wrote:

i have object of TEST as shown below:
ELEMENT *inventors=bibl io->AddChild("Invs ");
ELEMENT *t2;
is it better to initialize while declaring itself?
i mean,if i say :

ELEMENT *t2= inventors->AddChild("Inv" );// is this safe enough?

or do i need to go with usual conventions like:
ELEMENT *t2=NULL;
t2= inventors->AddChild("Inv" );

so please help me which one is better and safe?


There is no difference at all.
It all depends on the return value of AddChild if the pointer
is valid or not. In both cases.

'Prefer initialization above assignment'

--
Karl Heinz Buchegger
kb******@gascad .at
Sep 19 '05 #2
thanks for ur reply Karl ,
is there any reason for 'Preferring initialization above assignment'.?
just curious to know about it.

Sep 21 '05 #3
nrhayyal wrote:

thanks for ur reply Karl ,
is there any reason for 'Preferring initialization above assignment'.?
just curious to know about it.


In the worst case there is no difference between initialization and assignment.
Example:

int j = 5;

------

int j;
j = 5;

I would expect both codes to produce identical code.

But: There may be a difference if the variable you define is of
some class type:

std::string Name( "Compiler" );

------

std::string Name;
Name = "Compiler";

Those 2 code snippets do different things. The first snippet uses
a special constructor to create the Name object with a specific value.
The second code snippet however, creates the Name object and initialises
it to an empty string. The following assignment then discards that empty
string and assigns it a new value.
So while in the first case, the whole process is a one-step procedure (create
object with a specific value) it becomes a two-step procedure in the second
case (create empty object, alter empty object).

And then there is of course the case, where assignment simply doesn't work, eg.
because the 'variable' you define is a constant. A constant can only be initialized
but not assigned, hence initialization is the only thing that will work:

const int j = 5; // works

-------

const int j;
j = 5; // does not work. j is const, hence the compiler hinders you
// to change its value.

So all in all: There is no advantage in assignment in all those cases, so why use it?
Get used to use initialization and you have less problems in the long run.

--
Karl Heinz Buchegger
kb******@gascad .at
Sep 21 '05 #4
> nrhayyalwrote:
i have object of TEST as shown below:
ELEMENT *inventors=bibl io->AddChild("Invs ");
ELEMENT *t2;
is it better to initialize while declaring itself?
i mean,if i say :

ELEMENT *t2= inventors->AddChild("Inv" );// is this safe enough?
What is biblio, is it a object? If it's ,you are doing wrong,
First you have to new up the object, then you can call
any function with in that class, using pointer,
Eg:
ELEMENT *inventors=new ELEMENT();
inventors->AddChild("Inv" );

Thanks,
Bangalore


Sep 21 '05 #5
On Wed, 21 Sep 2005 10:47:55 +0200, Karl Heinz Buchegger <kb******@gasca d.at>
wrote:
So all in all: There is no advantage in assignment in all those cases, so why use it?
Get used to use initialization and you have less problems in the long run.


Right. Also:

class A
{
public:
A(int) {} // Constructor taking an int
A(const A&) {} // Copy constructor
};

int main()
{
A a0(1);
A a1 = 2;
return 0;
}
When instantiating a0, the constructor taking an int is used in all cases.

When initializing a1, however, the semantic is A(A(2)), which technically
constructs a temporary object using the construtor taking an int, then passing
it to the copy constructor of a1.

-dr
Sep 21 '05 #6

Dave Rahardja wrote:
On Wed, 21 Sep 2005 10:47:55 +0200, Karl Heinz Buchegger <kb******@gasca d.at>
wrote:
So all in all: There is no advantage in assignment in all those cases, so why use it?
Get used to use initialization and you have less problems in the long run.


Right. Also:

class A
{
public:
A(int) {} // Constructor taking an int
A(const A&) {} // Copy constructor
};

int main()
{
A a0(1);
A a1 = 2;
return 0;
}
When instantiating a0, the constructor taking an int is used in all cases.

When initializing a1, however, the semantic is A(A(2)), which technically
constructs a temporary object using the construtor taking an int, then passing
it to the copy constructor of a1.

Hi,

Are you sure about that? Please, just test your example.

Best regards,
Bogdan Sintoma

Sep 22 '05 #7
Bogdan Sintoma wrote:

Dave Rahardja wrote:

Right. Also:

class A
{
public:
A(int) {} // Constructor taking an int
A(const A&) {} // Copy constructor
};

int main()
{
A a0(1);
A a1 = 2;
return 0;
}
When instantiating a0, the constructor taking an int is used in all cases.

When initializing a1, however, the semantic is A(A(2)), which technically
constructs a temporary object using the construtor taking an int, then passing
it to the copy constructor of a1.

Hi,

Are you sure about that? Please, just test your example.


Yes, he sure about that. And yes, he is right.
Your tests may show a difference, since the compiler is allowed to
optimize that temporary away. Nevertheless the semantics is indeed A(A(2))
which can easily be seen by making the copy constructor unaccessible. In this
case the compiler must refuse to compile this code, since it cannot use the
copy constructor (even if that copy constructor gets optimized away).

--
Karl Heinz Buchegger
kb******@gascad .at
Sep 22 '05 #8

Karl Heinz Buchegger wrote:
Bogdan Sintoma wrote:

Dave Rahardja wrote:

Right. Also:

class A
{
public:
A(int) {} // Constructor taking an int
A(const A&) {} // Copy constructor
};

int main()
{
A a0(1);
A a1 = 2;
return 0;
}
When instantiating a0, the constructor taking an int is used in all cases.

When initializing a1, however, the semantic is A(A(2)), which technically
constructs a temporary object using the construtor taking an int, then passing
it to the copy constructor of a1.

Hi,

Are you sure about that? Please, just test your example.


Yes, he sure about that. And yes, he is right.
Your tests may show a difference, since the compiler is allowed to
optimize that temporary away. Nevertheless the semantics is indeed A(A(2))
which can easily be seen by making the copy constructor unaccessible. In this
case the compiler must refuse to compile this code, since it cannot use the
copy constructor (even if that copy constructor gets optimized away).

--

And yes, you are right. My mistake.

Best regards,
Bogdan Sintoma

Sep 22 '05 #9

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

Similar topics

30
3426
by: Christian Seberino | last post by:
How does Ruby compare to Python?? How good is DESIGN of Ruby compared to Python? Python's design is godly. I'm wondering if Ruby's is godly too. I've heard it has solid OOP design but then I've also heard there are lots of weird ways to do some things kinda like Perl which is bad for me. Any other ideas?
110
9868
by: Mr A | last post by:
Hi! I've been thinking about passing parameteras using references instead of pointers in order to emphasize that the parameter must be an object. Exemple: void func(Objec& object); //object must be an object instead of
16
25401
by: sneill | last post by:
How is it possible to take the value of a variable (in this case, MODE_CREATE, MODE_UPDATE, etc) and use that as an object property name? In the following example I want 'oIcon' object to have the properties: mode1, mode2, and mode3. This seems simple but I can't quite figure it out... Any ideas anyone?
18
1599
by: hzmonte | last post by:
typedef int t_compare_func(const void *, const void *); struct node *tree_search(struct node *root, const void *keyy, t_compare_func *comp) { struct node *cur_item; int result; if (root == NULL) return NULL; cur_item = root; while (cur_item != NULL) {
13
2425
by: Max | last post by:
Hi There! I'm having a mysterious error right after I login using Forms Authentication in my ASP.NET app. Below is the error... Exception Details: System.NullReferenceException: Object reference not set to an instance of an object. The exception throws at the code that tries to set a property (String data type) at my Menu user control.
69
5534
by: fieldfallow | last post by:
Hello all, Before stating my question, I should mention that I'm fairly new to C. Now, I attempted a small demo that prints out the values of C's numeric types, both uninitialised and after assigning them their maximum defined values. However, the output of printf() for the long double 'ld' and the pointer of type void 'v_p', after...
5
4323
by: Johs32 | last post by:
I have a struct "my_struct" and a function that as argument takes a pointer to this struct: struct my_struct{ struct my_struct *new; }; void my_func(struct my_struct *new); I have read that there is no difference between giving this function a
75
3354
by: Amkcoder | last post by:
http://amkcoder.fileave.com/L_BitWise.zip http://amkcoder.fileave.com/L_ptr2.zip
50
3465
by: arunajob | last post by:
Hi all, If I have a piece of code something like this void main(void) { char * p1="abcdefghijklmn"; ............................................. }
0
7920
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
8347
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7973
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...
0
8220
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...
1
5718
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...
0
5394
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...
1
2358
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
1
1454
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1189
bsmnconsultancy
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...

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.