473,791 Members | 3,111 Online
Bytes | Software Development & Data Engineering Community
+ 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_v ar;
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
11 3905
Jacek Dziedzic <jacek@janowo-NOSPAM-.net> wrote in message news:<bj******* ***@korweta.tas k.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
5028
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 and in module file2.py there exist command import file1? This is not working in C#. pujo
2
3105
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: virtual String toString(); where String is defined as:
16
2845
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 semi-circular definitions and
4
8264
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 try to add the references to the dll's. Is there a way round this problem? In C++ one could use header files... What can I do here?
0
3328
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 to reproduce it outside of the context in which I encountered it, so this post is more to serve as a record and possible workaround for others than may encounter the same issue. There was no circular reference in the control, or through that
7
13526
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 dependencies). But then I started wondering how "easy" it would be to similarly make a NON-RUNTIME circular dependency between (implicitly linked) DLLs. Indeed authors like John Lakos, who focus on compile/link-time dependencies (not run-time),...
3
3746
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 get a Circular Reference" error and cannot reference the project. Is there any way to find out the what Method or Project is causing the problem If I am on Project A tring to add Project B - I checked that A doesn't include B and B doesn't...
0
1677
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 glitches in the massive, complex machines they use to smash atoms.
2
1859
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 enumerate the AssemblyNames from the GetReferencedAssemblies() collection.
0
9669
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9517
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,...
0
10428
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10207
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9997
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 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...
0
9030
agi2029
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...
1
7537
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 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...
0
5559
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4110
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.