473,387 Members | 1,700 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

static stuff

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

Jul 23 '05 #1
8 1617
On Thu, 30 Jun 2005 08:45:26 +0400, Jon Slaughter
<Jo***********@Hotmail.com> wrote:

[]
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)


Just think of inheritance in another way. Make your derived classes base
classes that are used for generation of "concrete" derived classes.
Somethink like this:

#include <string>
#include <iostream>

template<class B, char const* Name>
struct decorator : B
{
// forwarding ctors if needed
decorator() {}
template<class T1> decorator(T1 const& t1) : B(t1) {}
template<class T1, class T2> decorator(T1 const& t1, T2 const& t2) :
B(t1, t2) {}
// ...

std::string name() const { return Name; }
};

struct A_impl {};
struct B_impl {};

extern char const A_name[] = "A class";
extern char const B_name[] = "B class";

typedef decorator<A_impl, A_name> A;
typedef decorator<B_impl, B_name> B;

int main()
{
A a;
std::cout << a.name() << std::endl;
B b;
std::cout << b.name() << std::endl;
}

--
Maxim Yegorushkin
<fi****************@gmail.com>
Jul 23 '05 #2

"Maxim Yegorushkin" <fi****************@gmail.com> wrote in message
news:op.ss52vkea3nxwlp@home...
On Thu, 30 Jun 2005 08:45:26 +0400, Jon Slaughter
<Jo***********@Hotmail.com> wrote:

[]
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)


Just think of inheritance in another way. Make your derived classes base
classes that are used for generation of "concrete" derived classes.
Somethink like this:

#include <string>
#include <iostream>

template<class B, char const* Name>
struct decorator : B
{
// forwarding ctors if needed
decorator() {}
template<class T1> decorator(T1 const& t1) : B(t1) {}
template<class T1, class T2> decorator(T1 const& t1, T2 const& t2) :
B(t1, t2) {}
// ...

std::string name() const { return Name; }
};

struct A_impl {};
struct B_impl {};

extern char const A_name[] = "A class";
extern char const B_name[] = "B class";

typedef decorator<A_impl, A_name> A;
typedef decorator<B_impl, B_name> B;

int main()
{
A a;
std::cout << a.name() << std::endl;
B b;
std::cout << b.name() << std::endl;
}

--
Maxim Yegorushkin
<fi****************@gmail.com>


but this looks like it will store Name for each object created?

I will have many objects with the same class name

like I will have

typedef decorator<A_impl, A_name> A1;
typedef decorator<A_impl, A_name> A2;
typedef decorator<A_impl, A_name> A3;
typedef decorator<A_impl, A_name> A4;

so will the template store A_name 4 times(or atleast a pointer in each
object?) or will all references to A_name refer to only one location? (heh,
I will have to study up on templates to see what is exactly going on here...
I'm only familiar with the very basics of them(since I haven't programmed in
a very very long time)

Thanks

Jon


Jul 23 '05 #3
On Thu, 30 Jun 2005 09:22:26 +0400, Jon Slaughter
<Jo***********@Hotmail.com> wrote:

[]
but this looks like it will store Name for each object created?
No, Name is a pointer and it is not stored as a data member in objects,
rather it's hardcoded in code where you refer to it.
I will have many objects with the same class name

like I will have

typedef decorator<A_impl, A_name> A1;
typedef decorator<A_impl, A_name> A2;
typedef decorator<A_impl, A_name> A3;
typedef decorator<A_impl, A_name> A4;
Here you did not declare any objects, rather you declared A1..4 to be
aliases for the same type decorator<A_impl, A_name>.
so will the template store A_name 4 times(or atleast a pointer in each
object?) or will all references to A_name refer to only one location?


As I said the pointer is hardcoded. The string literal used to initialized
the pointer has external linkage and static storage duration, which means
there will be only one instance of the literal in your program.

--
Maxim Yegorushkin
<fi****************@gmail.com>
Jul 23 '05 #4

"Maxim Yegorushkin" <fi****************@gmail.com> wrote in message
news:op***************@localhost.localdomain...
On Thu, 30 Jun 2005 09:22:26 +0400, Jon Slaughter
<Jo***********@Hotmail.com> wrote:

[]
but this looks like it will store Name for each object created?
No, Name is a pointer and it is not stored as a data member in objects,
rather it's hardcoded in code where you refer to it.
I will have many objects with the same class name

like I will have

typedef decorator<A_impl, A_name> A1;
typedef decorator<A_impl, A_name> A2;
typedef decorator<A_impl, A_name> A3;
typedef decorator<A_impl, A_name> A4;


Here you did not declare any objects, rather you declared A1..4 to be
aliases for the same type decorator<A_impl, A_name>.


heh yeah... I see now(not sure what I said that)(I think I understand the
code now for the most part... maybe it was just to late last night).
so will the template store A_name 4 times(or atleast a pointer in each
object?) or will all references to A_name refer to only one location?
As I said the pointer is hardcoded. The string literal used to initialized
the pointer has external linkage and static storage duration, which means
there will be only one instance of the literal in your program.

--


ok, I think I see why. when you do "return name" in the template it is and
the template is used to create the class it just returns the address of that
string which was declared only once and has only one "instance".

Now I have a new question. I figured that, say, if I create a bunch of char
pointers and then all initalize them to the same string..

maybe something like

strcpy(p1, "Note")

then somewhere else

strcpy(p2, "Note")

etc...

is the compiler going to make one instance of Note? i.e. does it compact all
string literals in the program into a table and remove identical strings?

Thanks

Jon
Maxim Yegorushkin
<fi****************@gmail.com>

Jul 23 '05 #5
On Thu, 30 Jun 2005 18:11:52 +0400, Jon Slaughter
<Jo***********@Hotmail.com> wrote:

[]
Now I have a new question. I figured that, say, if I create a bunch of
char pointers and then all initalize them to the same string..
maybe something like

strcpy(p1, "Note")

then somewhere else

strcpy(p2, "Note")
The you'd probably get a SIGSEGV trying to strcpy to an uninitialized
pointer.

char const* p1 = "note";
char const* p2 = "note";

is the compiler going to make one instance of Note? i.e. does it compact
all
string literals in the program into a table and remove identical strings?


This is done in the linker. Modern GNU and M$ linkers do that.

--
Maxim Yegorushkin
<fi****************@gmail.com>
Jul 23 '05 #6

"Maxim Yegorushkin" <fi****************@gmail.com> wrote in message
news:op***************@localhost.localdomain...
On Thu, 30 Jun 2005 18:11:52 +0400, Jon Slaughter
<Jo***********@Hotmail.com> wrote:

[]
Now I have a new question. I figured that, say, if I create a bunch of
char pointers and then all initalize them to the same string..
maybe something like

strcpy(p1, "Note")

then somewhere else

strcpy(p2, "Note")
The you'd probably get a SIGSEGV trying to strcpy to an uninitialized
pointer.

char const* p1 = "note";
char const* p2 = "note";


heh, yeah, but was just some C++ pseudo code ;)
is the compiler going to make one instance of Note? i.e. does it compact
all
string literals in the program into a table and remove identical strings?
This is done in the linker. Modern GNU and M$ linkers do that.


Ok... so at most I would just be wasting a pointer for storage of those
address... though I think its better to use the literal address(instead of a
pointer to the literal address) like you have done.
--
Maxim Yegorushkin
<fi****************@gmail.com>


Thanks again.
Jon
Jul 23 '05 #7

"Maxim Yegorushkin" <fi****************@gmail.com> wrote in message
news:op.ss52vkea3nxwlp@home...
On Thu, 30 Jun 2005 08:45:26 +0400, Jon Slaughter
<Jo***********@Hotmail.com> wrote:

[]
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)


Just think of inheritance in another way. Make your derived classes base
classes that are used for generation of "concrete" derived classes.
Somethink like this:

#include <string>
#include <iostream>

template<class B, char const* Name>
struct decorator : B
{
// forwarding ctors if needed
decorator() {}
template<class T1> decorator(T1 const& t1) : B(t1) {}
template<class T1, class T2> decorator(T1 const& t1, T2 const& t2) :
B(t1, t2) {}
// ...

std::string name() const { return Name; }
};

struct A_impl {};
struct B_impl {};

extern char const A_name[] = "A class";
extern char const B_name[] = "B class";

typedef decorator<A_impl, A_name> A;
typedef decorator<B_impl, B_name> B;

int main()
{
A a;
std::cout << a.name() << std::endl;
B b;
std::cout << b.name() << std::endl;
}

--
Maxim Yegorushkin
<fi****************@gmail.com>


ok, I am wondering why you went through all the trouble of declaring the
template to have a base class?

I came up with this example:

template<char const *Name>
class test
{
public:
char const *Get_Name()
{
return Name;
}
}

// must be extern for some reason
extern char const Name[] = "Name";
int main()
{
test<Name> t;

// This line is invalid... not sure exactly why.. I suppose the compiler
doesn't internally treat "Name" as a char const variable or something and so
it doesn't really have an address(i..e its not treated as a pointer but a
literal I suppose)
test<"Name"> t;

cout << t.Get_Name();
}
now, can't I use that template in the creation of my new class(which would
have been my derived class before)? I mean
can't I do something like

class new_test : test<Name>
{

blah...
}

? I mean maybe not valid C++ code but surely I don' thave to really derive a
new class... I just copy the stuff in the template to the new class... so it
would look something like:

class new_test : test<Name>
{
public:
char const *Get_Name()
{
return Name;
}

blah...
}
I mean surely C++ has a way to extend a class from another template without
deriving it from an object created by the template? (i.e. I don't think I
will really need a base class if I use a template like that because the
template can be included directly in the class and act part of that class?)?

Thanks again.

Jon
Jul 23 '05 #8
On Fri, 01 Jul 2005 00:37:47 +0400, Jon Slaughter
<Jo***********@Hotmail.com> wrote:

[]
ok, I am wondering why you went through all the trouble of declaring the
template to have a base class?
You can have the template as a base class as well.
I came up with this example:

template<char const *Name>
class test
{
public:
char const *Get_Name()
{
return Name;
}
}

// must be extern for some reason
extern char const Name[] = "Name";
int main()
{
test<Name> t;

// This line is invalid... not sure exactly why.. I suppose the compiler
doesn't internally treat "Name" as a char const variable or something
and so
it doesn't really have an address(i..e its not treated as a pointer but a
literal I suppose)
test<"Name"> t;

cout << t.Get_Name();
}
This code compiles fine. Add #include <iostream>, a semicolon after test
class definition and std:: before cout.
now, can't I use that template in the creation of my new class(which
would
have been my derived class before)? I mean
can't I do something like

class new_test : test<Name>
{

blah...
}

? I mean maybe not valid C++ code but surely I don' thave to really
derive a
new class... I just copy the stuff in the template to the new class...
so it
would look something like:

class new_test : test<Name>
{
public:
char const *Get_Name()
{
return Name;
}

blah...
}


Yes, you can do it as well.

--
Maxim Yegorushkin
<fi****************@gmail.com>
Jul 23 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Thomas Mlynarczyk | last post by:
Hello, Is there a way to access a static variable defined inside a function from outside that function? Something like the following: function foo() { static $bar = 'stuff'; // more code }
1
by: baylor | last post by:
In C#, an interface cannot mark any method as static. i'm told the ILASM supports it but i've never tested that Two questions. First, why? OK, i've heard the reason about interfaces being...
3
by: Steven T. Hatton | last post by:
Sorry about the big code dump. I tried to get it down to the minimum required to demonstrate the problem. Although this is all done with GNU, I believe the problem I'm having may be more general. ...
13
by: Alex Vinokur | last post by:
I have the following problem. class Base { public: void (*pfunc) (int) = 0; }; class Derived : public Base {
1
by: Jeff | last post by:
Hello everybody, I have a question concerning function declarations. When exactly do you have to declare functions if you want to use them? I have two functions main() and foo(), respectively...
33
by: Chris Capel | last post by:
What is the rationale behind the decision not to allow abstract static class members? It doesn't seem like it's a logically contradictory concept, or that the implementation would be difficult or...
6
by: GG | last post by:
Is this public static method thread safe. //Receives a file name as a parameter //and returns the contents of that file as a string public static string FileToStr(string fileName) { FileStream...
1
by: Ray Ackley | last post by:
I'm experiencing a threading problem that's really perplexing me. I have a multithreaded application that reads car ECU information that's sent over the serial port by the ECU and received by the...
11
by: comp.lang.php | last post by:
function blah($item) { if (!isset($baseDir)) { static $baseDir = ''; $baseDir = $item; print_r("baseDir = $baseDir\n"); } $dirID = opendir($item); while (($fyl = readdir($dirID)) !== false)...
5
by: Andy B | last post by:
I have a class that I want to make static but it uses some objects that are instance objects. I keep getting a compiler error saying something about using instance objects in a static class or...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.