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(ca use 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("Derived 1 class", Base::name); // or is it Derived1::name?
}
Get_Name(char *s)
{
strcpy(s,name); // or whatever
}
};
class Derived2 : Base
{
public:
Derived2()
{
strcpy("Derived 2 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 8 1640
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_imp l, A_name> A;
typedef decorator<B_imp l, 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>
"Maxim Yegorushkin" <fi************ ****@gmail.com> wrote in message
news:op.ss52vke a3nxwlp@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_imp l, A_name> A; typedef decorator<B_imp l, 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_imp l, A_name> A1;
typedef decorator<A_imp l, A_name> A2;
typedef decorator<A_imp l, A_name> A3;
typedef decorator<A_imp l, 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
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_imp l, A_name> A1; typedef decorator<A_imp l, A_name> A2; typedef decorator<A_imp l, A_name> A3; typedef decorator<A_imp l, A_name> A4;
Here you did not declare any objects, rather you declared A1..4 to be
aliases for the same type decorator<A_imp l, 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>
"Maxim Yegorushkin" <fi************ ****@gmail.com> wrote in message
news:op******** *******@localho st.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_imp l, A_name> A1; typedef decorator<A_imp l, A_name> A2; typedef decorator<A_imp l, A_name> A3; typedef decorator<A_imp l, A_name> A4;
Here you did not declare any objects, rather you declared A1..4 to be aliases for the same type decorator<A_imp l, 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>
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>
"Maxim Yegorushkin" <fi************ ****@gmail.com> wrote in message
news:op******** *******@localho st.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
"Maxim Yegorushkin" <fi************ ****@gmail.com> wrote in message
news:op.ss52vke a3nxwlp@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_imp l, A_name> A; typedef decorator<B_imp l, 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
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> This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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
}
|
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
allocated on the stack but that's not a real reason. Which is OK because this
isn't a real question, it's just a complaint dressed up to look like a reason
Second question. If a language doesn't support a fairly obvious feature, one
has to wonder if...
|
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. Someone on the SuSE
programming mailing list suggested my problem is that I'm trying to execute
a function (I assume he meant the constructor) at compile time. The same
source code compile if I don't try to split it up into separate libraries.
...
|
by: Alex Vinokur |
last post by:
I have the following problem.
class Base
{
public:
void (*pfunc) (int) = 0;
};
class Derived : public Base
{
|
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 defined in main.c and foo.c. There is no declaration
of foo() inside main.c, but main() calls foo().
If I compile both files (using gcc's -c option), and then link them, main()
calls foo() without any problem. How come ? Wasn't I suppose to declare...
| |
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 near-impossible. It seems like
it would be useful. In fact, there's a place in my code that I could make
good use of it. So why not?
Chris
|
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 fStream=null;
lock(fStream) //just in case we use it for multithreading to be thread
safe
{
|
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
laptop/program.
I'll try to give a concise synopsis as the program is easily 10k+
lines.
main.cs - contains the application start function. Here's the entire
code (minus misc junk) as it's rather small -
|
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) {
if (is_dir("$baseDir/$fyl")) blah($item);
|
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 method is not allowed. How do
you do this if you really need a static class but also have to use these
instance objects in them? If you need a simple example of what I am trying
to do, it is below:
imports Data.EternityRecordsEntities
namespace...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |