Friend - VC++ refusing to acknowledge

A

A L

Hi,

I using new() from a factory class to create instances of my product
classes. I have made the factory class a friend of each product class
and made the constructors of all product classes "protected" to
prevent their indiscriminate instantiation here and there using new().
However, the Visual C++ compiler says that the product class has no
constructor - even though it has the constructor defined in the
protected section. Any help?
 
V

Victor Bazarov

I using new() from a factory class to create instances of my product
classes. I have made the factory class a friend of each product class
and made the constructors of all product classes "protected" to
prevent their indiscriminate instantiation here and there using new().
However, the Visual C++ compiler says that the product class has no
constructor - even though it has the constructor defined in the
protected section. Any help?

Please read FAQ 5.8 and follow its recommendations. The problem is
likely on the line 42 of your header.

V
 
A

Alf P. Steinbach

* A L, on 15.06.2010 14:36:
Hi,

I using new() from a factory class to create instances of my product
classes. I have made the factory class a friend of each product class
and made the constructors of all product classes "protected" to
prevent their indiscriminate instantiation here and there using new().
However, the Visual C++ compiler says that the product class has no
constructor - even though it has the constructor defined in the
protected section. Any help?

Well, to help with future problems first: I think you really should follow
Paul's original advice and take a look at the FAQ item about how to post a
question about Code That Does Not Work.

That includes creating

A

*minimal*

and

*complete*

example that exhibits the problem.

Often just trying to create that example will tell you what the problem /
mistake / whatever is, and if not, then it helps others to help you.

No pain, no gain, as they used to say once.

That said, factories are sometimes appropriate, but they are *seldom* appropriate.

Chances are your factories are justa way to duplicate a lot of essentially
identical code.

Eliminating the factories may eliminate the problem... :) (Of course it's
better to learn something new, but sometimes all one needs is for the problem to
go away: learning is important but not that important!)

Anyways, try to create the above mentioned example, and please do take a look at
the FAQ, cause that may help in the future also. If creating the example does
not help, post it here.


Cheers & hth.,

- Alf
 
G

Goran

Hi,

I using new() from a factory class to create instances of my product
classes. I have made the factory class a friend of each product class
and made the constructors of all product classes "protected" to
prevent their indiscriminate instantiation here and there using new().

Hmm... If you don't want "new", why didn't you hide "new", then?

Goran.
 
M

mingze zhang

Hi,

I using new() from a factory class to create instances of my product
classes. I have made the factory class a friend of each product class
and made the constructors of all product classes "protected" to
prevent their indiscriminate instantiation here and there using new().
However, the Visual C++ compiler says that the product class has no
constructor - even though it has the constructor defined in the
protected section. Any help?

You are wrong. The following code apparently compiles and there're no
error messages at all.

class A
{
friend A *f();
protected:
A() { };
};
A *f()
{
return new A();
};

int main()
{
A* a = f();
}
 
A

A L

Hi,

It was an error in my code. I have solved it.

you really should follow Paul's original advice and take
a look at the FAQ item about how to post a
question about Code That Does Not Work.

Please give me a link to that FAQ.

Rgds/AL
 
A

A L

Hi,

Hmm... If you don't want "new", why didn't you hide "new",
then?

I have hidden all constructors of all my product classes - so they
cannot be instantiated with new. However, I made prototype factory a
friend of each product class (it has access to non-public members of
the product classes) so it *can* use new to create prototypes
initially. That's the only one single place I have used new. The
solution to totally hide new() without having to make the prototype
factory a friend of each class came to me later. I'll implement it
once I have completed the current task of my project.
 
A

A L

Hi,

You are wrong. The following code apparently compiles and
there're no error messages at all.

Yes, as I posted just now, there was an error in my code and I have
solved it.

Thx.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,190
Latest member
Martindap

Latest Threads

Top