meaning of class type

Hi. I know what it means to say type int or bool or char but what does it mean to say class type. For example

class Myclass {}

Myclass variableName;

So how memory interprets and access variableName? What does it mean to say variableName is of type Myclass?

Thanks...New to OOP
class Myclass {}; creates a new type called Myclass.
I know that but what does it mean actually? in terms of memory access of that region where class resides?
I don't think I understand your question. If you create an object of type Myclass ...

 
Myclass variableName;

then variableName will of course take up some memory (all objects do). If you want to know the size of the object in memory you can use sizeof.

 
std::cout << "Size of variableName: " << sizeof(Myclass) << std::endl;
It simply means variableName is an instance or an object of type MyClass.
in terms of memory access of that region where class resides?

A class is identical to a struct (except for default visibility of members).
As such, a class or struct always has an address that it resides at in memory. That address is determined by how the class or struct is allocated, such as is it global or local (within a function) or is it allocated dynamically from the heap.

The compiler knows the type and offset of each of the members of the class or struct and therefore the total size of the class or struct. Given the base address of the class or struct and the offset of each member, then compiler can always generate the appropriate instruction to reference each member.

1
2
3
4
5
6
7
struct foo
{   int a; // offset 0 from base of struct 
    int b; // offset 4 from base of struct (assumes 32 bit ints) 
    long c;  // offset 8 from base.
};  // This is a declaration only and does not have an address or take any space

foo bar;  // Now we have an object of type foo named bar that has an address and takes up space 

Last edited on
Thanks..It is clear to me now, but one more q just for clarification...If it were class instead of struct as above would the variables in constructor be allocated memory sequentially?
Last edited on
would the variables in constructor be allocated memory sequentially?

A constructor is a function that is called when an object is instantiated.

To answer what I think you were asking, yes, the variables in the class are allocated sequentially subject to alignment rules for the data types and the platform.

Topic archived. No new replies allowed.