imported_Tarzan

Junior Member
Sep 11, 2004
14
0
0
In C++ or any programming language if we define a variable then it carries a garbage value if not assigned.
Similarly what would be the default value if an object is not assigned.

I was working with intellij editor it was prompting an error ( if i did not assign a value to primitive data types, but in case of an object it was not showing any error) so what could be the default value of an object or why it was not prompting an error in case of object.

kindly clarify
:confused:
 

CTho9305

Elite Member
Jul 26, 2000
9,214
1
81
int foo;
cout << foo; //compiler warning
myClass bar;
cout << bar.toString(); //no warning

The reason it's not a problem is that class myClass has a constructor, and when you do "myClass bar;", it calls the constructor silently behind your back. If you look at the definition of myClass, you should see:
myClass::myClass() //constructor
{
//there may be some code here
}

If, on the other hand you did:
myClass *foobar;
cout << foobar->toString();
You'd crash when you run it, because you created a pointer foobar which points nowhere, and then dereferenced it. To make that work, you do:

myClass *foobar;
foobar = new myClass; // allocates space for an instance of myClass, calls the constructor
cout << foobar->toString();

This should then work without any problems.


Cliffs notes:
Classes have constructors which get called behind your back. Primitives don't.
 

torpid

Lifer
Sep 14, 2003
11,631
11
76
Never seen that before. I would think that myClass bar would simply throw an error.
 

DaveSimmons

Elite Member
Aug 12, 2001
40,730
670
126
Originally posted by: torpid
Never seen that before. I would think that myClass bar would simply throw an error.
Only if the constructor has required parameters. Ohterwise that line creates a perfectly valid object that is not null (it exists on the stack).

...and many classes have multiple constructors, requiring zero or more parameters, for example:

CMyTime() ; // creates and initializes to current OS time
CMyTime( time_t time ) ; // creates and initializes to time
CMyTime( int h, int m, int s, int m, int d, int y ) ; // hour is 0-23
 

Barnaby W. Füi

Elite Member
Aug 14, 2001
12,343
0
0
Originally posted by: amdfanboy
Yet another confusing C++ thread

Uh, things do tend to be confusing when you don't know them. I could say Java's confusing, but I don't know it, so that'd be pretty inane.
 

WannaFly

Platinum Member
Jan 14, 2003
2,811
1
0
Originally posted by: amdfanboy
Yet another confusing C++ thread

How could this be confusing? amd, you seem to know alot of other languages, specifically java. Unless you are not familiar with classes and constructors and how pointers work, that shouldn't be confusing.

And, if you dont know C++, learn it. It'll help you in the long run :p
 

VirtualLarry

No Lifer
Aug 25, 2001
56,572
10,208
126
Originally posted by: CTho9305
int foo;
cout << foo; //compiler warning
myClass bar;
cout << bar.toString(); //no warning

The reason it's not a problem is that class myClass has a constructor, and when you do "myClass bar;", it calls the constructor silently behind your back.

Cliffs notes:
Classes have constructors which get called behind your back. Primitives don't.

And for classes that are not meant to be directly constructed, or are only incompletely constructed in the base class, then protected constructors are a good idea too. (If you wanted an implicit constructor call to generate a compilier error.)
 

AFB

Lifer
Jan 10, 2004
10,718
3
0
Originally posted by: WannaFly
Originally posted by: amdfanboy
Yet another confusing C++ thread

How could this be confusing? amd, you seem to know alot of other languages, specifically java. Unless you are not familiar with classes and constructors and how pointers work, that shouldn't be confusing.

And, if you dont know C++, learn it. It'll help you in the long run :p

Ohh I already knew about this. I had to learn it the hard way when I was debugging which is probably the best way to do it. Basic C++ isn't that hard (I'm sure it has more down the road) but I feel like I need to take a shower after using it.

BTW, I am not a Java fanboi , I just like the pretty syntax and the refference model it uses for objects instead of the C++ refference/pointer system. I don't mind using it. I just find that it takes more code to do things in C++ that it does in Java and it is harded to follow(Probably my fault ;)).
 

sciencewhiz

Diamond Member
Jun 30, 2000
5,885
8
81
Originally posted by: BingBongWongFooey
Originally posted by: CTho9305
That's why C is cool. Nothing goes on behind your back ;)

Things go on behind your back in every language, just to different extents.

Even Assembly. Like the addresses for the labels that you branch to.
 

Barnaby W. Füi

Elite Member
Aug 14, 2001
12,343
0
0
It's just one of those personal threshold things. When something is below your threshold, you say it's helping you by taking care of the details. When it goes beyond your threshold, all of a sudden you say it's doing things "behind your back." But I've used the phrase before myself. :p I usually say that weakly-typed languages convert types "behind your back." But honestly, whichever language you're using, you should understand, and if you understand <insert weakly-typed language here>, then you'll know that it'll convert between types. Although it can happen unexpectedly, which sort of adds to the perception of it being sneaky.