473,473 Members | 1,844 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

class-in-a-typedef-in-a-class circular trouble

Hello!

I'm creating a class that has a static method which would
compute a lookup table needed later by all members of the class.
The problem is, the lookup table is composed of records and one
of the record fields is the class itself, i.e. I'm aiming at
something like:

typedef struct {
my_class instance;
int lookup_value;
} lookup_record;

class my_class {
private:
int some_internal_var;
static lookup_record lookup_table[1000];
public:
static void prepare_lookup_table();
// ...
};

but that won't compile with "my_class is used as a type but is
not defined as a type". All right then, the compiler doesn't
know yet, what my_class is. So I tried to add a line with

class my_class;

above it all, but then it says that "field my_class has
incomplete type". If I try to put the typedef after the
class, then the class declaration does not know what the
typedef is. Also classes and typedefs won't accept extern.

What do I do? This all is, btw, in a header file.

tia,
- J.

Jul 19 '05 #1
11 3873

"Jacek Dziedzic" <jacek@janowo-NOSPAM-.net> wrote in message
news:bj**********@korweta.task.gda.pl...
Hello!

I'm creating a class that has a static method which would
compute a lookup table needed later by all members of the class.
The problem is, the lookup table is composed of records and one
of the record fields is the class itself, i.e. I'm aiming at
something like:

typedef struct {
my_class instance;
int lookup_value;
} lookup_record;

class my_class {
private:
int some_internal_var;
static lookup_record lookup_table[1000];
public:
static void prepare_lookup_table();
// ...
};

but that won't compile with "my_class is used as a type but is
not defined as a type". All right then, the compiler doesn't
know yet, what my_class is. So I tried to add a line with

class my_class;


You need to tell the compiler the size of the class also then.
How is to deduce the size just by looking at a forward declaration.
So one way to solve your problem is to store a pointer to my_class in your
struct instead of the object itself.
my_class *pInstance;.
This way compiler can live just with the forward declaration (i.e.without
seeing it's actual definition)

HTH,
J.Schafer

Jul 19 '05 #2
Josephine Schafer wrote:

You need to tell the compiler the size of the class also then.
How is to deduce the size just by looking at a forward declaration.
I thought that a forward declaration would tell the compiler to
"look somewhere else for the precise class declaration and
compute the size", but it seems it's not *that* smart, right?
So one way to solve your problem is to store a pointer to my_class in your
struct instead of the object itself.
my_class *pInstance;.
This way compiler can live just with the forward declaration (i.e.without
seeing it's actual definition)


Yes, I thought about this, but I don't think I can afford such
overhead in memory and in speed (it's a look-up table supposed
to speed things up, after all) that would be introduced by
one extra dereferencing. I think I'll just forget about class
neatness and store the internal class variable (an int)
in the lookup record instead of storing the class itself.
That won't look good, but would be effective.

I was hoping for a solution along the lines of "extern class",
one that would tell the compiler to look for the precise
class definition somewhere else (later in the code).

thanks,
- J.

Jul 19 '05 #3
"Jacek Dziedzic" <jacek@janowo-NOSPAM-.net> wrote in message
news:bj**********@korweta.task.gda.pl...
| I'm creating a class that has a static method which would
| compute a lookup table needed later by all members of the class.
| The problem is, the lookup table is composed of records and one
| of the record fields is the class itself, i.e. I'm aiming at
| something like:
|
| typedef struct {
| my_class instance;
| int lookup_value;
| } lookup_record;
|
| class my_class {
| private:
| int some_internal_var;
| static lookup_record lookup_table[1000];
| public:
| static void prepare_lookup_table();
| // ...
| };
| but that won't compile [....]

Suggestion:
- Make lookup_table a static global in your .cpp file,
instead of a private static member of my_class.
Or better, use an anonymous namespace in the .ccp file
that will include the definition of both lookup_record
and the lookup_table:
namespace {
typedef struct { ..... } lookup_record;
lookup_record lookup_table[1000];
}

Alternatively, change the type of the look-up table:
static lookup_record* lookup_table;
(as Josephine suggested)
You then need to allocate the table in your initialization
function, using new[]:
lookup_table = new lookup_record[1000];
.... and it might be a good idea to free it before
program exit: delete[] lookup_table;
The first solution is nicer/simpler IMO.

This said, this fixed size table (1000 elements) seems
arbitrary. Changing the type of the look-up table to
std::vector<my_class> or std::map<my_class,int> would
probably be a good idea...

hth,
Ivan
--
http://ivan.vecerina.com
Jul 19 '05 #4
Ivan Vecerina wrote:
Suggestion:
- Make lookup_table a static global in your .cpp file,
instead of a private static member of my_class.
Or better, use an anonymous namespace in the .ccp file
that will include the definition of both lookup_record
and the lookup_table:
namespace {
typedef struct { ..... } lookup_record;
lookup_record lookup_table[1000];
}

Yes, I think I might try that. I've got it all inside
a (named) namespace already, which I stripped for brevity here.
I guess it won't interfere?
This said, this fixed size table (1000 elements) seems
arbitrary. Changing the type of the look-up table to
std::vector<my_class> or std::map<my_class,int> would
probably be a good idea...


It was arbitrary, for it was a simplified example only.
In reality the lookup table would have about a million
elements. The size is known at compile time, and the
look-up table will have to be transferrable between
processors via MPI -- that's why a std::vector is not
a good idea -- it can't be copied "memcpy-wise",
whereas a traditional array can be.

Thanks for the suggestions,
- J.

Jul 19 '05 #5
On Thu, 11 Sep 2003 12:16:49 +0000, Jacek Dziedzic
<jacek@janowo-NOSPAM-.net> wrote:
Hello!

I'm creating a class that has a static method which would
compute a lookup table needed later by all members of the class.
The problem is, the lookup table is composed of records and one
of the record fields is the class itself, i.e. I'm aiming at
something like:

typedef struct {
my_class instance;
int lookup_value;
} lookup_record;

class my_class {
private:
int some_internal_var;
static lookup_record lookup_table[1000];
public:
static void prepare_lookup_table();
// ...
};

but that won't compile with "my_class is used as a type but is
not defined as a type". All right then, the compiler doesn't
know yet, what my_class is. So I tried to add a line with

class my_class;

above it all, but then it says that "field my_class has
incomplete type". If I try to put the typedef after the
class, then the class declaration does not know what the
typedef is. Also classes and typedefs won't accept extern.

What do I do? This all is, btw, in a header file.


Do it the other way around:

struct lookup_record;

class my_class {
private:
int some_internal_var;
static lookup_record lookup_table[1000];
public:
static void prepare_lookup_table();
};

struct lookup_record
{
my_class instance;
int lookup_value;
};

lookup_record my_class::lookup_table[1000];

static members can have incomplete types.

Tom
Jul 19 '05 #6
Hi Jacek,
"Jacek Dziedzic" <jacek@janowo-NOSPAM-.net> wrote in message
news:bj**********@korweta.task.gda.pl...
| Ivan Vecerina wrote:
| > Suggestion:
| > - Make lookup_table a static global in your .cpp file,
| > instead of a private static member of my_class.
| > Or better, use an anonymous namespace in the .ccp file
| > that will include the definition of both lookup_record
| > and the lookup_table:
| > namespace {
| > typedef struct { ..... } lookup_record;
| > lookup_record lookup_table[1000];
| > }
| >
|
| Yes, I think I might try that. I've got it all inside
| a (named) namespace already, which I stripped for brevity here.
| I guess it won't interfere?
An unnamed namespace may be nested within another namespace, this
won't be a problem (if that's what you meant by "interfere").
(it just generates longer identifiers for the linker, no big deal).

| The size is known at compile time, and the
| look-up table will have to be transferrable between
| processors via MPI -- that's why a std::vector is not
| a good idea -- it can't be copied "memcpy-wise",
| whereas a traditional array can be.
Note that an std::vector always allocates contiguous storage,
so the whole contents can be copied by memcpy (if items are POD).
But if the data is strictly fixed-size, no need to bother.

hth, Ivan
--
http://ivan.vecerina.com
Jul 19 '05 #7
Thanks a lot, Ivan! Your namespace idea worked just fine!

- J.

Jul 19 '05 #8
Amazingly simple, that!

thanks,
- J.

Jul 19 '05 #9
Jacek Dziedzic <jacek@janowo-NOSPAM-.net> wrote in message news:<bj**********@korweta.task.gda.pl>...
Hello!

I'm creating a class that has a static method which would
compute a lookup table needed later by all members of the class.
The problem is, the lookup table is composed of records and one
of the record fields is the class itself, i.e. I'm aiming at
something like:
What is struct lookup_record good for? Why do you not put it's member
`int lookup_value' inside my_class?

Try get a clear idea of the responsibilities of lookup_record and my_class.
From the code snippet you've given you can not look-up a instance of
my_class without already having a reference to that class!?

typedef struct {
my_class instance;
int lookup_value;
} lookup_record;


This is C, in C++ you define a struct exactly the same way as a class.

struct lookup_record {
my_class instance;
int lookup_value;
};

regards, Stephan
Jul 19 '05 #10
Jacek Dziedzic <jacek@janowo-NOSPAM-.net> wrote in message news:<bj**********@korweta.task.gda.pl>...
Josephine Schafer wrote:

You need to tell the compiler the size of the class also then.
How is to deduce the size just by looking at a forward declaration.


I thought that a forward declaration would tell the compiler to
"look somewhere else for the precise class declaration and
compute the size", but it seems it's not *that* smart, right?


No, it can't do that. Every translation unit (roughly, a translation
unit is a source file and all its includes) is compiled separately. As
each TU is compiled, the compiler has no knowledge whatsoever of any
other TU. When you forward declare a class, the class definition does
not have to appear later in the same TU (it can be in a different -
one that's the point :-) ) even though that happens to be the case in
your example. So in general, the "somewhere else" you want the
compiler to look is not available.
So one way to solve your problem is to store a pointer to my_class in your
struct instead of the object itself.
my_class *pInstance;.
This way compiler can live just with the forward declaration (i.e.without
seeing it's actual definition)


Yes, I thought about this, but I don't think I can afford such
overhead in memory and in speed (it's a look-up table supposed
to speed things up, after all) that would be introduced by
one extra dereferencing. I think I'll just forget about class
neatness and store the internal class variable (an int)
in the lookup record instead of storing the class itself.
That won't look good, but would be effective.


Be wary of confusing your design to save speed unless and until you
have proven that the "slow" way is too slow to be acceptable.
Premature optimisation being the root of all evil and all that.

GJD
Jul 19 '05 #11
"Jacek Dziedzic" <jacek@janowo-NOSPAM-.net> wrote in message
news:bj**********@korweta.task.gda.pl...
| Thanks a lot, Ivan! Your namespace idea worked just fine!
I'm glad it worked.

While there are alternatives (kudos to Tom, I forgot about
the solution he mentioned), I think it is usually a good
idea to move private static members into an anonymous
namespace in the implementation file. Especially when this
data involves additional type declarations.
This way there are completely hidden from users, and
will cause less compile time overhead (and less
recompilations when they are modified).

Cheers,
Ivan
--
http://ivan.vecerina.com
Jul 19 '05 #12

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

Similar topics

9
by: ajikoe | last post by:
Hello, I have two modules (file1.py and file2.py) Is that ok in python (without any weird implication) if my module import each other. I mean in module file1.py there exist command import file2...
2
by: ernesto basc?n pantoja | last post by:
Hi everybody: I'm implementing a general C++ framework and I have a basic question about circular dependencies: I am creating a base class Object, my Object class has a method defined as:...
16
by: Kiuhnm | last post by:
Is there an elegant way to deal with semi-circular definitions? Semi-circular definition: A { B }; B { *A }; Circular reference: A { *B }; B { *A }; The problems arise when there are more...
4
by: pnp | last post by:
I'm developing an app (in C #) that uses 2 usercontrols that must be in different dll's. The problem is that each one needs to use the other, so as a result I get a circular reference error when I...
0
by: Alan Samet | last post by:
Before telling me what I already know about what this error means, please read the post. I encountered this bizarre error when running aspnet_compiler.exe. Unfortunately, I don't know of a way...
7
by: barias | last post by:
Although circular dependencies are something developers should normally avoid, unfortunately they are very easy to create accidentally between classes in a VS project (i.e. circular compile-time...
3
by: =?Utf-8?B?c2lwcHl1Y29ubg==?= | last post by:
Hi I Have a solution with about 50 projects and each project have References to 1 to n of the projects in the solution. I try go to a project and try to add a reference to another project and I...
0
by: mrchatgroup | last post by:
news from http://www.mrchat.net/myblog/myblog/small-accidents-mean-big-trouble-for-supercollider.html Small Accidents Mean Big Trouble for Supercollider Image Scientists expect startup...
2
by: Dansk | last post by:
Hi all, I am currently writing some code that explores assemblies dependencies. I start loading the first assembly with Assmebly.LoadFrom which gives me an Assembly instance. Then, I...
0
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...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
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...
1
isladogs
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...
0
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...
0
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...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
bsmnconsultancy
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...

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.