DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Page 1 of 2 12 LastLast
Results 1 to 15 of 29

Thread: Is a variable an object?

  1. #1
    Join Date
    Jun 2007
    Posts
    6

    Is a variable an object?

    This is probably a dumb question but I'm really new to C++. I understand that a class creates a custom type. And when you instantiate it, you make an instance of the object. This is obviously very similar to creating a simple variable, so is a variable considered an object as well? Fundamental types don't seem to have member functions or data members like objects.

    Yes, btw, I have searched for an answer to this before I posted. I've actually been searching for a week and have gotten mixed answers.

  2. #2
    Join Date
    May 2007
    Posts
    843
    Yes is the same.

  3. #3
    Join Date
    Nov 2003
    Posts
    4,118
    These terms are slightly overalpping. An object in the C++ world usually refers to an instance of a class, and sometimes it's called a class object to make the intention clearer. In the C world, an object is a variable or a constant.
    When reading text in "standardese" objects have the collective meaning of class objects and variables, i.e., a chunk of a memory that contains some value. In less official texts, for example newsgroup posts, one usually uses 'object' in the sense of a class object, and 'variable' for any POD type such as int, char etc.
    BTW, this isn't a dumb question at all.
    Danny Kalev

  4. #4
    Join Date
    Jan 2007
    Posts
    145
    They are cosidered same. However for a complex class C
    C a;
    C b;

    a=b (can throw exceptions,make your program crash)
    however int a ,b;
    a=b; will always work.

  5. #5
    Join Date
    Nov 2003
    Posts
    4,118
    a=b; //assuming these are class objects
    Doesn't have to throw. It depends on how the copy-ctor, assignment operators etc. are implemented.

    as for assigning two variables: surprisingly, you can have an exception here too (a trap actually) if one of the variables is a NaN for example. Therefore, this generalization is at best very loose.
    Danny Kalev

  6. #6
    Join Date
    Mar 2007
    Posts
    13
    as Danny said, they're both a chunk of memory.

    Well, you probably know what a constructor is,
    so you can say:

    classA myObject(5,"something"); //for initialization of object

    BUT

    you can also say
    int a(10); //which is pretty much an initialization of int. Same as int=10;


    So you see on both cases i'm initializing the Objects through constructors.
    Argumentation the "=" operator for initialization is overwritten.
    Last edited by etenv; 06-15-2007 at 04:41 PM.

  7. #7
    Join Date
    Jun 2007
    Posts
    6
    So is it the same with all the types? The book I'm learning from seems to only refer to strings and vectors as objects since they have member functions. Int, char, float, etc. don't have member functions or data members within themselves do they?

    as Danny said, they're both a chunk of memory.

    Well, you probably know what a constructor is,
    so you can say:

    classA myObject(5,"something"); //for initialization of object

    BUT

    you can also say
    int a(10); //which is pretty much an initialization of int. Same as int=10;


    So you see on both cases i'm initializing the Objects through constructors.
    Argumentation the "=" operator for initialization is overwritten.
    When you assign an argument to the constructor in myObject, could you instead put classA myObject=5 just like a variable? Also, that value in the constructor would be a data member, so does that mean that int has a data member that holds its value?

    Sorry for all the questions but I'm trying to understand this all more clearly rather than just memorizing it.

  8. #8
    Join Date
    Mar 2007
    Posts
    13
    no that kind of assignment (ClassA myObject=5 )wont go. The Operator "=" is only for "primary" types.

    BTW,
    classA myObject(5,"something")
    should be
    classA classA(5,"something")

    Sorry, my bad. :)

  9. #9
    Join Date
    Nov 2003
    Posts
    4,118
    Actually, you *can* write Myclass a=5;

    if Myclass has a constructor that takes int as an argument, and that constructor isn't declared explicit. Look for further information on explicit constructors on DevX. I've written several tips and articles about this topic.

    To summarize: all variables are objects, but programmers usually restrict the former to non-class objects, and the latter to class objects.
    The more precise distinction in the C++ literature is: POD types and non-POD types.
    Danny Kalev

  10. #10
    Join Date
    Jun 2007
    Posts
    6
    Okay, if that's the case, I'm a little confused with what my book is saying then. It says, "You can store objects in variables, just like with built-in types." Does that mean that the name (or identifier) is the variable and the value of it is the object?

    no that kind of assignment (ClassA myObject=5 )wont go. The Operator "=" is only for "primary" types.

    BTW,
    classA myObject(5,"something")
    should be
    classA classA(5,"something")

    Sorry, my bad.
    Why are you assigning an object with the same name as the class?

  11. #11
    Join Date
    Mar 2007
    Posts
    13
    Quote Originally Posted by zeeenza
    Okay, if that's the case, I'm a little confused with what my book is saying then. It says, "You can store objects in variables, just like with built-in types." Does that mean that the name (or identifier) is the variable and the value of it is the object?



    Why are you assigning an object with the same name as the class?
    Sorry if i confused you. There is no particular reason. The only reason was that i wasn't focused and that i was thinking on something else while writing. So anyway, don't consider it important.
    But it's still something legal to do. a Class can have an Object with the same name, although you wouldn't want to have the same names for classes and objects in 99,9% of the cases :)

  12. #12
    Join Date
    Dec 2004
    Location
    San Bernardino County, California
    Posts
    1,468
    Would that compile? My guess is no. I could see if you have "Object object" but not "Object Object".

  13. #13
    Join Date
    Mar 2007
    Posts
    13
    it compiled just fine with Dev-C++. My guess is it should compile anywhere.
    myClass::myClass, where is the problem?

  14. #14
    Join Date
    Dec 2004
    Location
    San Bernardino County, California
    Posts
    1,468
    I guess the compiler is smarter than I am. I would expect I could not be sure if the programmer was referring to the class or the object.

  15. #15
    Join Date
    Dec 2003
    Posts
    3,366
    there is a concept of atomic data types which basically are data types that your CPU can *directly* manipulate: this is 8,16,32,64 bit integers and 32,64 and 80 bit floats for most common processors. (Whether your high level language abstracts them into objects or not). There will be some terminology difference from book to book, but these are not objects in the OOP sense.

Similar Threads

  1. object variable not set
    By tim dwight in forum VB Classic
    Replies: 1
    Last Post: 10-31-2002, 09:16 AM
  2. OO Considered Harmful
    By Richard Dalton . in forum Architecture and Design
    Replies: 33
    Last Post: 07-30-2001, 02:11 AM
  3. Object Variable Sharing
    By Cameron Frasnelly in forum VB Classic
    Replies: 2
    Last Post: 07-05-2000, 10:31 AM
  4. Re: Passing an object name through a variable
    By mrfelis in forum VB Classic
    Replies: 9
    Last Post: 04-25-2000, 10:08 PM
  5. Object Variable Not Set
    By John K. in forum ASP.NET
    Replies: 1
    Last Post: 03-28-2000, 07:20 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center