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

Home Posts Topics Members FAQ

inaccessible constructor

I have the following C++ declaration:

struct S {

private:

S();

};

struct T {

S s;

};

Why isnt it illegal C++?
It gives the false impression that a member function of S can
instantiate
an object of the type T.

If an object of type T cannot be constructed under any
circumstance, then
why allow the type declaration to pass in the first place?

Aug 4 '06 #1
7 3975

cp********@yahoo.com napísal(a):
I have the following C++ declaration:

struct S {

private:

S();

};

struct T {

S s;

};

Why isnt it illegal C++?
It is illegal, i.e. the gcc output:

x.cc: In constructor 'T::T()':
x.cc:8: error: 'S::S()' is private
x.cc:12: error: within this context
It gives the false impression that a member function of S can
instantiate
an object of the type T.

If an object of type T cannot be constructed under any
circumstance, then
why allow the type declaration to pass in the first place?
Aug 4 '06 #2
Because the "s" in struct T hasn't been initial. When compile meet
something like: "T t", then it look for the comstructor for S, and
report an error.
cp********@yahoo.com wrote:
I have the following C++ declaration:

struct S {

private:

S();

};

struct T {

S s;

};

Why isnt it illegal C++?
It gives the false impression that a member function of S can
instantiate
an object of the type T.

If an object of type T cannot be constructed under any
circumstance, then
why allow the type declaration to pass in the first place?
Aug 4 '06 #3
cp********@yahoo.com wrote:
I have the following C++ declaration:

struct S {
private:
S();
};

struct T {
S s;
};

Why isnt it illegal C++?
You're mistaken. It will cause a compile error if you try to
instantiate T.
It gives the false impression that a member function of S can
instantiate
an object of the type T.
No it doesn't. If you try to instantiate T, you'll get a compile
error.
If an object of type T cannot be constructed under any
circumstance, then
why allow the type declaration to pass in the first place?
You're mistaken.

Try compiling the following, then please report back on what your
compiler says:

struct S {
private:
S();
};

struct T {
S s;
};

int main()
{
T t;
}
Here's what Comeau online says:

"ComeauTest.c", line 7: error: "S::S()" is inaccessible
S s;
^
detected during implicit generation of "T::T()" at line 12

1 error detected in the compilation of "ComeauTest.c".

Hope that helps.

Best regards,

Tom

Aug 4 '06 #4
All that is done mate. Please explain why only the following should
compile?

struct S {
private:
S();
};

struct T {
S s;
};

If it compiles, then obviously the language designers have thought that
T is useful in some future context.

Care to explain what that might be apart from
creating pointers, assigning null to them and
comparing them (which could be accomplished
just as well with void pointers)?

Thomas Tutone wrote:
cp********@yahoo.com wrote:
I have the following C++ declaration:

struct S {
private:
S();
};

struct T {
S s;
};

Why isnt it illegal C++?

You're mistaken. It will cause a compile error if you try to
instantiate T.
It gives the false impression that a member function of S can
instantiate
an object of the type T.

No it doesn't. If you try to instantiate T, you'll get a compile
error.
If an object of type T cannot be constructed under any
circumstance, then
why allow the type declaration to pass in the first place?

You're mistaken.

Try compiling the following, then please report back on what your
compiler says:

struct S {
private:
S();
};

struct T {
S s;
};

int main()
{
T t;
}
Here's what Comeau online says:

"ComeauTest.c", line 7: error: "S::S()" is inaccessible
S s;
^
detected during implicit generation of "T::T()" at line 12

1 error detected in the compilation of "ComeauTest.c".

Hope that helps.

Best regards,

Tom
Aug 4 '06 #5
cpp_nov...@yahoo.com wrote:
All that is done mate.
OK. Then didn't you post the results like I asked? I'm taking the
trouble to answer your question. Won't you do me the courtesy of
responding to my reasonable request?
Please explain why only the following should
compile?

struct S {
private:
S();
};

struct T {
S s;
};
Because you didn't instantiate T. Until you do so, your compiler
doesn't see the error. Other compilers may issue a diagnostic there.
Yours doesn't. Either live with it or switch compilers.
If it compiles, then obviously the language designers have thought that
T is useful in some future context.
Are you ABSOLUTELY sure? Is it POSSIBLE that the compiler writer for
the compiler you were using decided it was easier not to check such
things until you tried to instantiate the class in question, since it
would only be a problem if you instantiated the class?
Care to explain what that might be apart from
creating pointers, assigning null to them and
comparing them (which could be accomplished
just as well with void pointers)?
Different compilers exhibit different behaviors. If you want a
compiler that gives you a diagnostic for the code you posted without
instantiating T, then switch to a compiler that does so, or ask the
writer of your compiler to make that change. In fact, I'm sure that if
you offer them enough money for their services, the compiler writers
will be happy to make that change for you. But that seems like a poor
use of your money, if you ask me. If you try to instantiate T, you get
a compile error, and if you don't try to instantiate T, then your
program never tries to access the inaccessible constructor.

Best regards,

Tom

Aug 4 '06 #6
su*****@gmail.com wrote:
Because the "s" in struct T hasn't

See below.

Brian

--
Please don't top-post. Your replies belong following or interspersed
with properly trimmed quotes. See the majority of other posts in the
newsgroup, or the group FAQ list:
<http://www.parashift.com/c++-faq-lite/how-to-post.htm>
Aug 4 '06 #7
cp********@yahoo.com wrote:
All that is done mate.
See below.

Brian

--
Please don't top-post. Your replies belong following or interspersed
with properly trimmed quotes. See the majority of other posts in the
newsgroup, or the group FAQ list:
<http://www.parashift.com/c++-faq-lite/how-to-post.htm>
Aug 4 '06 #8

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

Similar topics

0
by: Uzytkownik | last post by:
I don't know where and why the error is. Klasa.h: class Klasa1 { protected: int A, D, M, T; public: Klasa1(int a = -1, int d = -1, int m = -1, int t = -1); Klasa1(Klasa1&);
7
by: spam | last post by:
The following code does not compile: class X {}; class Y : private X {}; class Z : public Y { public: Z(X&) {} // problem here
6
by: Marty McDonald | last post by:
public class BaseDALC { //Don't allow instantiation private BaseDALC() // To compile properly, I had to change this to "protected" { } } .... public class StatisticsDALC : BaseDALC {
1
by: Larry | last post by:
I have a VB background and am developing a new windows app in Csharp. I'm getting the error. 'inaccessible due to its protection level' I've added a TextBox1 and a Button1 to a form. I...
1
by: Thomas Barnet-Lamb | last post by:
I was wondering if anyone could give me some help with the following. Consider the code snippet: struct qqq{typedef qqq* pointer;}; template<class al> struct foo : public al { template...
1
by: Chris | last post by:
Hi, New to C# programming. I'm trying to implement some simple security in my website. Basically a user cannot surf to secured aspx pages simply by accessing them directly through the address...
3
by: tshad | last post by:
I am getting a message for my objects that say: testNulls.cs(13,33): error CS0122: 'FtsData.IntType.IntType()' is inaccessible due to its protection level I have a class calling objects out of...
1
by: jheled | last post by:
Staring with g++ 3.4, this code results in an error ---------------------------------- struct A { }; class B : private A { };
15
by: Victor Bazarov | last post by:
Hello, Take a look at this program: ----------------------------------- class B { B(const B&); B& operator=(const B&); public: B(int);
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
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...
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: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
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 ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.