DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Page 2 of 2 FirstFirst 12
Results 16 to 29 of 29

Thread: Is a variable an object?

  1. #16
    Join Date
    Mar 2007
    Posts
    13
    well that's something new to me.
    Up till now i was convinced that this distinction was being made through vptrs and vtables...

    So you're saying they don't come in play as well, right?

    And another question. How can i understand whether the processor has this feature, except by trying directly? I mean are there some macros for that purpose??

  2. #17
    Join Date
    Jun 2007
    Posts
    6
    Ok so now I'm confused again. Are variables considered objects, or are they not? I'm getting mixed answers.

  3. #18
    Join Date
    Mar 2007
    Posts
    13
    short answer: they are :)

  4. #19
    Join Date
    Dec 2004
    Location
    San Bernardino County, California
    Posts
    1,468
    Bjarne Stroustrup's glossary pages, at
    http://www.research.att.com/~bs/glossary.html

    defines your terms as:

    variable - named object in a scope. TC++PL 2.3.1, 10.4.3, D&E 2.3.

    object - (1) a contiguous region of memory holding a value of some type. (2) a named or unnamed variable of some type; an object of a type with a constructor is not considered an object before the constructor has completed and is no longer considered an object once a destructor has started executing for it. Objects can be allocated in static memory, on the stack, on on the free store. TC++PL 4.9.6, 10.4, 10.4.3, D&E 2.3, 3.9.

    So, according to the "father" of C++, a variable is an object

  5. #20
    Join Date
    Nov 2003
    Posts
    4,118
    short answer: objects are also variables, variables aren't objects. This is not the official, standardese terminology. In standardese, the term "variable" is hardly used. Rather, it uses the term objects collectively.
    As I said, most programmers treat "variables" as instances of built-in types:
    int a; //a is a variable

    and objects as instances of a class:

    std::string s;//s is an object.

    However, a is also an object. I wouldn't call s a variable, though.
    Last edited by Danny; 06-18-2007 at 08:11 AM.
    Danny Kalev

  6. #21
    Join Date
    Dec 2003
    Posts
    3,366
    But now, some built in types (string, vector, etc) *are* objects and others are atomics/variables (int, float). And what about arrays? Not a class and not atomic, what is it?

    But to end your confusion: an object can be safely referred to as a variable to your peers and most of the community (except in the most formal of writing projects, such as a published paper or book) who's type might be atomic or class or array or whatever. A variable can also be called an object with minimal confusion under the same circumstances. Or you can mix and match them as seems most appropriate for the type being discussed. If you plan to write a book or do something formal, I suggest you define the terms in the introduction and stick to your provided definitions.

    As to how you can be sure a processor has a specific type of support, that would be in your assembly language book for that processor. I am unaware of any macros you could try but you can make a variable of each size from 8 bits to 64 and beyond as your compiler supports and each floating point type. Then generate the assembly language for your code and if any of the types seem to be declared in an odd fashion they may not be supported on the CPU. I can assure you that any normal CPU (computer CPU) supports the types I mentioned (perhaps without 64 bit integer support), if the CPU has no floating point support (small embedded chips occasionally have this issue, or *very* old computers) you may not have those. The definition I gave is not concerned with high level constructs (really, I was just describing the word atomic which is not used much) but you could make a definition that does include vtables etc.
    Last edited by jonnin; 06-18-2007 at 03:09 PM.

  7. #22
    Join Date
    Nov 2003
    Posts
    4,118
    As I said earlier, the precise terms are POD types vs. non-POD types which roughly correspond to variables and objects respectively.
    Danny Kalev

  8. #23
    Join Date
    Jun 2007
    Posts
    6
    Okay...Well it seems some people think of them as the same and some don't. I guess it really doesn't matter...They both work in a similar way and I'll never need to know what makes a built-in type work the way it does. I just have 2 more questions.

    If a variables is, in fact, an object, do they have any member functions?

    And

    As I said before, the book I'm learning from says "You can store objects in variables, just like with built-in types." What exactly does this mean?

  9. #24
    Join Date
    Dec 2003
    Posts
    3,366
    No, they do not have "member functions" exactly, you can consider the "math" operators to be "member functions" but they are not really "functions" because the underlying hardware supports these operations (no "code" exists to do the task). A computer system has to support some basic things to function: movement of data (assignment), addition (everything in math can be done with adding if you support negative numbers, multiply is just add in a loop, divide is subtract in a loop, etc). Conditional branching (if statements, etc) and loops (not required but wow its hard without them!). A lot more stuff is supported to make life easy.

    so.. that statement from your book ... ignore it. It does not mean anything really. I suspect he simply means you make instances of classes such as:
    string str;
    str is the variable that the object is stored in (possibly) with very loose terminology.

  10. #25
    Join Date
    Nov 2003
    Posts
    4,118
    I really don't know what that statement in the book means, but it sounds confusing and sloppy at best.
    As for member functions: I wouldn't rely on this distinction because objects don't have to have member functions in order to be considered as such. How about this object?

    struct A
    {
    private:
    int x;
    public:
    int y;
    } x;

    x is not a POD type because it has a private member. It has no overt member functions either.
    Danny Kalev

  11. #26
    Join Date
    Nov 2003
    Posts
    4,118
    Quote Originally Posted by etenv
    it compiled just fine with Dev-C++. My guess is it should compile anywhere.
    myClass::myClass, where is the problem?
    A class and an object can't have the same name. More generally, types and objects must have different names.
    MyClass::MyClass() is not an object. It's an expression that creates an unnamed (temporary) object of type MyClass. The unnamed object is immediately destroyed after the evaluation of this expression. But we're digressing from the main topic here...
    Danny Kalev

  12. #27
    Join Date
    Nov 2003
    Posts
    4,118
    It's time to resolve the mystery and see how the C++ defines these two terms:
    An object is a region of storage. [...] A function is not an object, regardless of whether or not it occupies storage in the way that objects do.] [] The properties of an object are determined when the object is created.
    What about variables?
    3-4. variable is introduced by the declaration of an object. The variable's name denotes the object.
    So, an object is a piece of memory and a variable is the name that refers to an object. This however is the C++ standard. In informal texts, variables mean instances of built-in types and objects are instances of classes.
    Danny Kalev

  13. #28
    Join Date
    Mar 2007
    Posts
    13
    I'm starting to love that thread! :D

  14. #29
    Join Date
    Jun 2007
    Posts
    6
    Okay, I think I understand it now. As I said before, I like to understand this stuff instead of just memorizing it. So I'll probably be back with more questions in the future. Thanks for all the help guys. :WAVE:

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