473,654 Members | 3,082 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to forward declare a class in namespace?

Hello gurus,

I stuck in following: how can I do forward declaration if the forward
declared class is in some namespace?

something like

// header

class std::string; // approach#1

namespace std
{
class string; // approach#2
};

struct A
{
A();
~A();
std::string * p;
};

both approaches does not compiled.
So, How can I do it?

Thanks

Nov 29 '05 #1
4 13244
Following compiles well:
namespace std_
{
class string; // approach#2
};

struct A
{
std_::string * p;
};

So the problem is not in forward decalration in namespace. Actually
std::string is not a class but a typedef to template basic_string
instantiated with type char. Therefore forward declaration fails. So I
think there is no legal way to forward declare std::string.
Only classes accessed through <iostream> can be forward declared in
STL. One can use <iosfwd> to do this.

Nov 29 '05 #2
yu***@gmx.de wrote:
Hello gurus,

I stuck in following: how can I do forward declaration if the forward
declared class is in some namespace?

something like

// header

class std::string; // approach#1

namespace std
{
class string; // approach#2
};

struct A
{
A();
~A();
std::string * p;
};

both approaches does not compiled.
So, How can I do it?

Thanks


Approach #2 works in general. See this example, which compiles just fine
(compiling using cygwin: cygming special):

namespace ns
{ struct A; }

struct B
{ ns::A* a; };

namespace ns
{ struct A {}; }

int main()
{
B b; // "unused variable", I know
return 0;
}

You problem is that you got the type of std::string wrong. So, you
create a conflict between you saying
namespace std { class string; }
and the <string> header saying something else.
Consider this example, which also works:

// header.hpp
namespace std
{
template<typena me _CharT, typename _Traits, typename _Alloc> class
basic_string;
typedef basic_string<ch ar> string;
}
struct B
{ std::string* s; };

// main.cpp
#include <string>
#include "header.hpp "

int main()
{
B b;
return 0;
}

In the forward declaration I use the declaration which is also used in
the <string> library header.

Sadly, the above is still implementation-dependent. I would appreciate
some enlightenment how to do this really non-implementation-dependent.

Gabriel
Nov 29 '05 #3
Dervish wrote:
Following compiles well:
namespace std_
{
class string; // approach#2
};

struct A
{
std_::string * p;
};

So the problem is not in forward decalration in namespace. Actually
std::string is not a class but a typedef to template basic_string
instantiated with type char. Therefore forward declaration fails. So I
think there is no legal way to forward declare std::string.
Only classes accessed through <iostream> can be forward declared in
STL. One can use <iosfwd> to do this.


Why is that?
Nov 29 '05 #4
On 2005-11-29, Gabriel <ab***@127.0.0. 1> wrote:
// header.hpp
namespace std
{
template<typena me _CharT, typename _Traits, typename _Alloc> class
basic_string;
typedef basic_string<ch ar> string;
}
struct B
{ std::string* s; };

// main.cpp
#include <string>
#include "header.hpp "

int main()
{
B b;
return 0;
}

In the forward declaration I use the declaration which is also
used in the <string> library header.

Sadly, the above is still implementation-dependent. I would
appreciate some enlightenment how to do this really
non-implementation-dependent.


It can't be done. The implementation is allowed to include extra
trailing template parameters in the basic_string template, so no
forward declaration of std::basic_stri ng will be portable.

A proxy class that contains a std::string could be used instead.

class string_proxy;

struct b
{
string_proxy *s;
};

string_proxy could be defined in your .cpp file.

#include <string>

class string_proxy
{
std::string s_;
public:
string_proxy(co nst std::string& s): s_(s) { }
std::string& operator() { return s_; }
const std::string& operator() const { return s_; }
};

The above is a simple-minded example; you might want something
with an automatic conversion to std::string, or implement the
subset of the std::string interface that you need for class B.

--
Neil Cerutti
Nov 29 '05 #5

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

Similar topics

1
10232
by: amit gulati | last post by:
Is it possible to forward declare a template class using "class XXX". If yes, then what is the syntax??
9
2651
by: Divick | last post by:
Hi all, does any one know what is the right way to forward declare classes within namespaces. Though I have been using the syntax as follows but it doesn't sound good to me. namespace myVeryOwnNamespace { class myClass1; }
4
11794
by: Sebastien Tardif | last post by:
Subject: XmlSerializer.Deserialize complain when root declare the namespace If I do XmlSerializer.Deserialize( myString ) and myString is: String myString = "<?xml version=\"1.0\" encoding=\"utf-16\"?><DocumentResponse ><documentSize xmlns=\"urn:webservices.docharbor.com\">23</documentSize></DocumentResponse>"; It's working
23
3841
by: mark.moore | last post by:
I know this has been asked before, but I just can't find the answer in the sea of hits... How do you forward declare a class that is *not* paramaterized, but is based on a template class? Here's what I thought should work, but apparently doesn't: class Foo; void f1(Foo* p)
3
8855
by: yancheng.cheok | last post by:
hello all, how can i make, a forward declaration class's enum member, being visible by another class? consider the following case, ---------------------------- dog.h ----------------------------
1
3294
by: yancheng.cheok | last post by:
currently, i have a private function in cat named privateFun. i would like to have this function "private" to all except dog's action member function. by using the following approach, all the dog's members can access all the cat's private members. // cat.h //
8
2139
by: Ole Nielsby | last post by:
I want to create (with new) and delete a forward declared class. (I'll call them Zorgs here - the real-life Zorks are platform-dependent objects (mutexes, timestamps etc.) used by a cross-platform scripting engine. When the scripting engine is embedded in an application, a platform-specific support library is linked in.) My first attempt goes here: ---code begin (library)---
6
7886
by: matrem | last post by:
I wish to make a method of a base class a friend of a method of a derived class. However, this makes me quickly run into a circular dependency problem. I must implement the base class first, but it doesn't know anything about methods of the derived class. How can I forward declare a method of the derived class? Hopefully this code will clarify the problem: void B::h(); // error ?? class A { friend void B::h(); void f() {}; void...
1
1249
by: bokavan | last post by:
i have windows form app(Vc++ 2005) 2 text box1,2 2 button1,2 i have class person {have get, set (string ,int)} and i want to declare person p; to see it in both two push button1,2 my problem is i declare person p; inside buttons when buttons end
0
8285
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
8814
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
8706
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
8591
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
7304
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...
0
5621
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();...
0
4149
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...
1
1915
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1592
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 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...

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.