J
Jon Slaughter
Is there any way to declare a member of a base class to make the itself in
the derived class to be static?
i.e., I have two classes
class Base
{
protected:
char *name;
};
class Derived1 : Base
{
public:
Get_Name(char *s)
{
strcpy(s,name); // or whatever
}
};
class Derived2 : Base
{
public:
Get_Name(char *s)
{
strcpy(s,name); // or whatever
}
};
the problem is that each derived object allocates a name pointer and for my
application each object of the derived class will all have the same name(the
name represents the name of the class)
but if I did this
class C
{
private:
static char name[] = "C Class";
public
Get_Name(char *s)
{
strcpy(s, C::name);
}
}
class Q
{
private:
static char name[] = "Q Class";
public
Get_Name(char *s)
{
strcpy(s, Q::name);
}
}
then there is only one name pointer for all the objects of the C class...
but he problem is that I will have many different types of classes that all
inherent from a base class and I want to be able to differentiate them by
there name... but I don't want to have to declare a static variable in each
of the different class declerations(cause lets say I forget one).
I'm pretty sure that if I do this:
class Base
{
protected:
static char *name;
};
class Derived1 : Base
{
public:
Derived1()
{
strcpy("Derived1 class", Base::name); // or is it Derived1::name?
}
Get_Name(char *s)
{
strcpy(s,name); // or whatever
}
};
class Derived2 : Base
{
public:
Derived2()
{
strcpy("Derived2 class", Base::name); // or is it Derived2::name?
}
Get_Name(char *s)
{
strcpy(s,name); // or whatever
}
};
that both the derived classes will be stepping over each others shoes.
Basicaly I want to have some static members of derived classes but force
them to be declared and to be static in an elegant way(and not by declaring
the member static in all those classes I will have to create)
Also I am wondering that if I declare an array as const does that gaurantee
that it cannot be changed? (i.e. does the compiler stick it in some type of
read only memory or something?)
Thanks,
Jon
the derived class to be static?
i.e., I have two classes
class Base
{
protected:
char *name;
};
class Derived1 : Base
{
public:
Get_Name(char *s)
{
strcpy(s,name); // or whatever
}
};
class Derived2 : Base
{
public:
Get_Name(char *s)
{
strcpy(s,name); // or whatever
}
};
the problem is that each derived object allocates a name pointer and for my
application each object of the derived class will all have the same name(the
name represents the name of the class)
but if I did this
class C
{
private:
static char name[] = "C Class";
public
Get_Name(char *s)
{
strcpy(s, C::name);
}
}
class Q
{
private:
static char name[] = "Q Class";
public
Get_Name(char *s)
{
strcpy(s, Q::name);
}
}
then there is only one name pointer for all the objects of the C class...
but he problem is that I will have many different types of classes that all
inherent from a base class and I want to be able to differentiate them by
there name... but I don't want to have to declare a static variable in each
of the different class declerations(cause lets say I forget one).
I'm pretty sure that if I do this:
class Base
{
protected:
static char *name;
};
class Derived1 : Base
{
public:
Derived1()
{
strcpy("Derived1 class", Base::name); // or is it Derived1::name?
}
Get_Name(char *s)
{
strcpy(s,name); // or whatever
}
};
class Derived2 : Base
{
public:
Derived2()
{
strcpy("Derived2 class", Base::name); // or is it Derived2::name?
}
Get_Name(char *s)
{
strcpy(s,name); // or whatever
}
};
that both the derived classes will be stepping over each others shoes.
Basicaly I want to have some static members of derived classes but force
them to be declared and to be static in an elegant way(and not by declaring
the member static in all those classes I will have to create)
Also I am wondering that if I declare an array as const does that gaurantee
that it cannot be changed? (i.e. does the compiler stick it in some type of
read only memory or something?)
Thanks,
Jon