views:

317

answers:

9

Given this:

struct { int x; } ix;

struct A { A() {}; int x; };
A ia;

Which of these is true?

a. ix is an object
b. ia is an object
c. both are objects
d. both are not objects.
+1  A: 

The word "object" is a rather ambiguous specification without some more context, but in general objects have identity, behavior, and state.

Neither ix nor ia have all three; ix fails because it lacks identity or behavior, and ia fails because it has no behavior. Both are essentially just blobs of data.

John Feminella
Why does an object need behavior. I think state is enough to classify you as an object.
Martin York
that is what I thought, but my instructor insists A has a constructor and that an instance of a class/struct constructed makes it an object.
Murali
@Martin: I don't agree. `int x = 5;` allows `x` to hold state, but is an `int` (the primitive) an object?
John Feminella
@John : heh, I just asked the same question on Martin's answer :P
Kornel Kisielewicz
@John: No int is NOT an object (it is a type). But x is an object.
Martin York
@Martin: I think you misunderstood what I meant -- I said "an `int`", that is, "an instance of an `int`".
John Feminella
Why does an object need state?
jalf
A: 

By my definition, I'd say an object has properties and methods. Both nouns and verbs.

You can kick a ball, you can invade a country, and you can eat, milk, or punch a cow. Those are therefore objects.

You might have a data structure that represents the properties of a ball (radius), country (population), or cow (daily milk output in liters), but that data structure doesn't represent an object in my mind until you tell it how to process pertinent behaviors.

I recognize this definition may not work in 100% of cases, but it's close enough for my needs.

Tenner
I use objects of type int to represent my cows. I only need to store thier weight in version 1.
Martin York
@Martin: Good choice, YAGNI.
Steve Jessop
A: 

Technically, an object is an instance of a class, but objects' true usefulness lies in their ability to encapsulate information and aid in the design of systems. They are an analysis tool.

calico-cat
+10  A: 

Many of these answers have ignored the C++ tag. In C++, "an object is a region of storage. [Note: a function is not an object, regardless of whether or not it occupies storage in the same way that objects do.]" (The C++ Standard, 1.8/1).

If the homework question is about C++, then no other definition of object is applicable, not even "anything that is visible or tangible and is relatively stable in form" (dictionary.reference.com). It's not asking for your opinion about OOP principles, it's in effect asking whether ix and ia are variables.

Since it's homework I'll not tell you the answer, but do note that struct { int x; } ix; is not the same thing as struct ix { int x; };.

On the other hand, if the homework assignment is about OOP principles, then knock yourself out with whatever definition your lecturer has given you of "object". Since I don't know what that is, I can't tell you what answer he'll consider correct...

Steve Jessop
It is a C++ intro class. My answer would be they both are objects in C++
Murali
+1: for the Standard lookup
Kornel Kisielewicz
Yes, that'd be my answer too. In fact, (a), (b) and (c) are all true, so it's pretty hard to give a wrong answer ;-). If the instructor disagreed with (c), I'd ask for better questions in future...
Steve Jessop
@Steve, yet that definition also states that `int x;` is an object :)
Kornel Kisielewicz
@Kornel: Correct. In C++, that `x` is an object.
Steve Jessop
@Steve: I know, I just pointed that out as amusing :)
Kornel Kisielewicz
So my question is: Is that why struct X{}; sizeof(X) == 1 and not 0.
Martin York
@Martin: It's not 0 because some data needs to be allocated in the instance of `struct empty{}; new empty; // needs to point to something`. http://www2.research.att.com/~bs/bs_faq2.html#sizeof-empty
GMan
@Martin: well, the proximate cause is that 9/3 says "Complete objects ... shall have nonzero size". The motivation is what Bjarne says, that GMan links to.
Steve Jessop
I know. Just though it was worth pointing out.
Martin York
@Martin: I thought it was strange you didn't know. :) Your comment was stated strangely. :p
GMan
This answer is technically correct, but misleading as well. I'd be surprised if an intro to C++ class used the C++ standard's definition of an "object".
jalf
+3  A: 

Given the C++ tag, the answer is pretty much "take your choice."

The C standard defines an object as meaning (in essence) anything that has an address, including all instances of native/primitive types (e.g. int). Since C++ depends so heavily on C, that definition still carries some weight in C++. By this definition, essentially every variable is an object, and so are a few other things (e.g. character string literals, dynamically allocated blocks of memory).

In Smalltalk (at rather the opposite extreme) the answer would be none of them is an object -- an object never has public data. Its behavior is defined entirely in terms of responses to messages.

Jerry Coffin
A: 

An object is an instnace of a type (be it POD or class).

As such you are able to extract the address of an object. All objects take up at least one byte. The reason for this is that you don't have to add special code for handling zero sized objects because every object in memory has a destinct address (by making everything at least one byte the compiler will automatically have a unique address for each object).

int main()
{
    struct { int x; } ix; 

    struct A { A() {}; int x; }; 
    A ia;

    ix.x = 5; // Assigned a value.
              // Thus it has state and thus is an object.

    ia.x = 6; // Assigned a value.
}

So they are both objects.

Martin York
so `int x = 6` is an object?
Kornel Kisielewicz
@Kornel: So x is an object.
Martin York
@Martin: amusing, yet correct :)
Kornel Kisielewicz
I believe that this code is ill-formed. A type with no linkage (an unnamed class) cannot be used to declare an entity with linkage (`ix` is declared with external linkage).
AndreyT
@AndreyT: it compiles fine on VC9, not that it means anything :>
Kornel Kisielewicz
GCC will also compile it, but Comeau reports an error "error: use of a type with no linkage to declare a variable with linkage". I believe Comeau is right (as usual) and that this is what 3.5/8 in the standard is trying to tell us. However, there is a problems with 3.5/8 - it talks about names and the class is unnamed in this case. Hmm...
AndreyT
I added some details about DR#132 to my answer. It is quite possible I'm wrong here.
AndreyT
Moved ix into main(). Now it does not have any linkage issues. So does that make it better?
Martin York
+1  A: 

These questions are impossible to answer without extra clarification. The question is tagged C++, which means that the language is supposedly C++.

In this case, if the declarations are made in namespace scope, the ix declaration is invalid. It is illegal to use an unnamed class type (which has no linkage) to declare an object with external linkage. The declaration of ix would work in local scope

void foo() {
  struct { int x; } ix; // OK, no linkage
}

It might also work if ix was declared with internal linkage at namespace scope

static struct { int x; } ix; // OK? Internal linkage?

although I personally believe that this was intended to be ill-formed as well (Comeau somehow allows it).

But a namespace-scope declaration with external linkage is ill-formed

// In namespace scope
struct { int x; } ix; // ERROR

So, if the namespace scope is assumed and if the above declarations are meant to be taken as a single piece of code, there are no meaningful answers to these questions. The whole code is simply invalid. It is meaningless. It is not C++.

Otherwise, if ix is declared with no linkage (local) or with internal linkage, then ix is an object.

As for ia, it is an object regardless of where it is declared, since the class type is named.

Note though that the notion of object in C++ has nothing to do with classes. Object in C++ is a region of storage (memory). A variable of int type is an object in C++, for one example.

Added later: The bit about legality of ix declaration is an interesting issue. Apparently C++98 allowed such declarations, which was proposed to be outlawed in DR#132. However, later the proposal was rejected (for a rather weird reason) and the things were left as is. Yet, Comeau Online refuses to accept a declaration of an object with external linkage with unnamed type (internal linkage is OK). It could quite possibly be a formal bug in Comeau compiler (not that I'd complain about it).

Added even later: Oh, I see that there's an even later DR#389, which finally outlaws such declarations, but the status of this DR is still CD1.

AndreyT
And this is why it is *vitally important* that anyone who knows C++ stay well clear of introductory C++ classes ;-)
Steve Jessop
A: 

The real answer is "e. Whoever writes code like this should be coached to improve their legibility." Okay, that was a joke.

This question isn't so complex. But elsewhere, I've seen programming tests written purposely complex for the purpose of seeing if you can solve puzzles. It's completely pointless, because code that is that complex shouldn't and usually does not exist. If it's that hard to read, it's poorly written code.

Remember, code is not written for computers. Code is written for the next developer after you to read and understand.

And don't write code just so it works. That's not a high enough standard. The worst junk in the world will run, but it's a nightmare to fix or upgrade.

ux9i
What does the complexity of code you've seen for programming tests *elsewhere* have to do with anything?
jalf
Which of these is true? a, b, c, d. That's a test question. And the code you would never see anywhere. It's formatted pooly, has no comments, etc. It's purposely complex in order to teach you something, force your eyeballs to go zig zag to get the concept. The fact that this is a prime example of an antipattern in the industry seems relevant.
ux9i
But your answer says "the code is bad. Actually it's not bad, but I've seen bad code elsewhere which was bad", and then it segues into a rant about *that*. This is not a "prime example" of anything, because you just so happened to say in your question that "this question isnt so complex". If your post is nothing more than an excuse to rant about complex code seen *elsewhere*, then it belongs on a blog, not here. And it is not a "prime example" if code elsewhere is an even better example.
jalf
+1  A: 

There are two commonly used definitions of "object" in C++.

One is official according to the C++ standard, and says that everything that has storage allocated for it is an object. A struct is an object, an int is an object, a bool is an object, a pointer is an object, a string literal is an object, and so on. By this definition, ix, ia and x are all objects. But this probably isn't what your teacher meant. You have to be a bit of a language lawyer to use this definition, and it's not that widely known among "average" C++ users. It's also not a very relevant definition for someone just learning the language.

The definition you are probably expected to use is that of an "object" in the object-oriented sense. Here (at least in the C++ family of languages), an object is typically meant to be an instance of a class.

Which leaves the next obvious question: Is an instance of a struct also an object? Depends. In C++, a class and a struct are essentially the same, so semantically, yes, but technically, you're not using the class keyword, so syntactically, probably not.

In short: It's a silly, and badly worded question, and only you know what your teacher means or wants to hear, because you're the one who attended the classes, not us. All we can do is guess at what he thinks defines a class.

jalf
How is it being a language-lawyer to use the C++ definition, but not being a language-lawyer to say that although there are two different keywords which can be used to define a class, "technically" if you use the one not spelled c-l-a-s-s, it's not really a class and hence its instances are not really objects? If the intent of the question is to teach the students that structs are *not* classes, then frankly the question can FRO ;-)
Steve Jessop
And OK, saying "an int is an object" wouldn't be popular in an "introduction to C++ and OOP" class. At least, not among people used to Java. Among people used to Smalltalk or Ruby, maybe a C++ int is a (poor, crippled) object, simply because objects is what things are. It's just a really rubbish object, with no members other than the ones implicitly invoked by operators ;-)
Steve Jessop
It's not a very helpful definition. The only reason C++ objects have a name at all is so that the standard has a simple way to refer to "stuff that has a type and is in memory". That's not helpful to people learning C++. It's not even useful to you or me, really. Except, again, as a way to be formal and refer back to the standard. Hence the language lawyer comment. You have to care about the precise wording, more than about what's actually useful to know, in order to find the C++ standard definition of "object" useful.
jalf
To someone learning C++'s flavor of OOP, the OOP meaning of "object" is relevant. And to someone learning C++ without touching on OOP, the word "object" shouldn't really need to feature at all
jalf
"the OOP meaning". Or at any rate *an* OOP meaning. As I said in my answer, if you think the question is about an OOP sense of object, then you need to know which one. We've seen at least three in different answers to this question. I genuinely don't think OOP meanings of object are useful to me either, which is why I don't mind at all that the C and C++ standards use the word as jargon, and I gratefully use the same jargon when talking about those languages.
Steve Jessop
I see where you're coming from, but you're forgetting the context of the question. This is a question asked in an "intro to C++" class. In those, OOP is a pretty common subject, and what's more, it is the C++ flavor of OOP, not the Smalltalk one or the Ruby one that is being taught. I tried to give the answer that would actually be useful to the OP. If we can look away from the ISO/IEC JTC1/SC22/WG21 document for two seconds, I'm willing to bet $20 that his teacher is not asking about objects in the C++ standard sense of the word, but in the "instance of a class" sense.
jalf
most of the answers and comments here are drifting off into academic masturbation, showing off everyone's prowess with the C++ standard, and nitpicking over the definition of OOP, none of which is relevant. Unless you are *seriously* suggesting that there is a high likelihood that by "object", his teacher meant "a region of storage", I don't see what relevance that definition has.
jalf
Well, the questioner says in a comment somewhere that "neither" was stated to be wrong. Assuming "both of them" is right, then it's impossible to tell which definition he's using, since it doesn't affect the answer. I've never taken an intro to C++ course, so I was seriously suggesting that, but of course I could have misjudged what's likely. IMO using OOP jargon in C++ is a waste of time unless comparing languages. The specific trouble I see with "object is instance of class" is that it leaves students misreading the standard, if they even see it. That doesn't mean it's not the defn used :-(
Steve Jessop
And of course I pity the poor OOP student who's exposed to Javascript, and concludes that it doesn't have objects because it doesn't have classes ;-) I think you have to use the right language for the context, and that means C++ objects have a different definition from Java objects, and both are different from Javascript objects. I'm prepared to believe that Murali's teacher disagrees with me.
Steve Jessop
@Steve: Agreed with your last point. I'm just saying that your typical OOP programmer working in C++ will most likely think of an object as "an instance of a class", and so that's probably the definition the teacher wants the answers to be based on. Probably. I think we can both agree that it's bad to mix up the definitions, and especially to use the word "object" without being clear about what's meant by it.
jalf