If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register or Login
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

C4677, mixing private and public ??

I’m creating a Windows Form application as a tool for website development. The initial form maintains a list of user defined website names paired with the local path to the files that make up the site. The path points to the base directory of the site and that base directory should contain certain sub-directories for use by the eventual process.

I’ve created a website class to maintain the data and path related to each website. This class implements IComparable so the list can be kept in alphabetic order:
website.h:

The main form calls the CheckDirectory method to insure the path and subdirectories exist. If any of the sub-directories is missing it pops up a form to ask if the user wants to create the missing directories.

I need to remember the reference to the website in the sub-form so the process can create the proper directories if the user requests them, but I keep getting warnings/error messages when I move the WebSite member variable around. The previous code has only warnings, and runs, but I’d like to find out the proper syntax so I can eliminate the warnings.

Re: C4677, mixing private and public ??

You get the C4677 warning because the parameter list of your NeedSubDirectories constructor, that is part of this public class' public interface, contains a parameter of type website ^, which is a private class (in the absence of a class accessibility specifier, C++/CLI defaults to private). (I have no idea, though, why you get the same warning for the declaration of the private member in line 26.) Since it probably isn't planned to ever access the classes in this assembly from outside of the assembly (and this is the only scenario where the trouble C4677 warns you of actually could arise), class accessibility doesn't really matter, so you can simply fix the warning by making website a public class.

BTW, the "mix of private and public" mentioned in the title of this thread is actually possible by using the C++/CLI-specific (i.e. unavailable in native C++) access specifier internal. A member with this visibility is public inside the assembly defining the class containing that member, but private to the outside world. However, this specifier can only be applied to members, not to classes.

BTW2, your implementation of website::CompareTo() never (exception see below) returns equality, not even when comparing a website instace to itself. I'm not sure that would ever actually cause trouble with any of the .NET collections, but I wanted to mention it. The exception: It does return equality (i.e. 0) when comparing to nullptr. And this obviously is wrong according to basic logic: Your website instance that CompareTo() is called on can in no way be nullptr itself. The reference implementation on MSDN (see http://msdn.microsoft.com/en-us/libr...omparable.aspx) returns 1 in this case, which makes sense, because it means that nullptr always is less than any existing instance of the class.

Also, I'm not entirely sure how C++/CLI interprets the C-style cast you apply to obj, but I suspect it turns it into a static_cast which is dangerous in case anyone happens to pass anything else than a website ^ or nullptr to your implementation. You may not want to get as fancy as the reference implementation and gcnew your own exception object, but I'd suggest to at least use a safe_cast instead that will throw an InvalidCastException on its own when someone calls that method, passing an incompatible object. (That is, BTW, one of the main reasons why I usually prefer to implement IComparable<T> rather than IComparable.)

I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.

This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.

I’m still lost on the initial question. When the website class performs it’s Check Directory function and determines that the sub-directory structure is incomplete, I need to pop up a form and ask the user if they want the sub-directory(s) built for them. The form actually builds the directories when the user says “build them”. In that case the form needs to know what to build and where to build them. That is all contained in the website class.

The call to the form uses “this” to pass the current website data to the form.

Despite the equal name, the generic and non-generic comparable interfaces are distinct, just like CompareTo(Object ^) and CompareTo(website ^) are distinct overloads of the same method (though inherited from distinct interfaces). You may very well implement both the generic and non-generic versions, though I myself never have seen a reason for that. AFAIK the default comparer used by the non-generic collections contains a CompareTo(Object ^) implementation that is able to make use of the generic CompareTo() implementation of the type it is applied to. (Can't find the place on MSDN where that is documented, though, at the moment.)

Casting is ok here. In fact, there's no way to avoid it in the non-generic CompareTo() implementation. The MSDN reference implementation, of course, does that too, though they use a dynamic_cast instead of the safe_cast I suggested. The difference between these two approaches is that their use of dynamic_cast will raise a NullReferenceException if an incompatible object is passed to CompareTo(), while the safe_cast will raise an InvalidCastException. You should always use either of these approaches since that is the only way to ensure runtime type-safety. (Actually, there are other ways to do that, but IMO they don't gain anything bersides complicating the code, so they are of rather academic interest.)

I’m still lost on the initial question. [...]

[...]

The code works as is, but gets the warnings. What is the proper syntax to eliminate the warnings?

I explained that right at the beginning of post #2: Turn website into a public refernece class instead of the private class it is now. I have demonstrated the change in the snippet at the beginning of this post.

I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.

This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.

Re: C4677, mixing private and public ??

Thanks Eri,

This site, and members like yourself, are extremely helpful.

Code:

public ref class website : public IComparable<website ^>

is the syntax that solved all my problems!

I’ve been a “C” programmer since the 1970’s using the first edition K&R C book as a guide. The second addition of their book was ANSI C. Before C I programmed in assembler and Fortran IV. Hence the “Papa” in my log on name.

I wrote my first Unix Windows application in “C” using structs to manage the buttons, labels, lists, (objects) long before they introduced classes in C++.

Moving to managed C++ to work with forms has been a chore, but the help I get on this site has made it much easier. But, I have to ask one last question: A lot of the developers that I work with are telling me to give up on managed C++ and move directly into C#. So, are they correct? Is C++ a dying language that is being replaced by C#? Should I abandon my current path and learn the C# syntax?

Thanks again for all your help. Plan A is to stick with C++, but I could be swayed by someone who knows BOTH languages.

Re: C4677, mixing private and public ??

Originally Posted by PapaGeek

Thanks Eri,

You're welcome!

I’ve been a “C” programmer since the 1970’s using the first edition K&R C book as a guide. The second addition of their book was ANSI C. Before C I programmed in assembler and Fortran IV. Hence the “Papa” in my log on name.

Interesting. Actually, I happened to use the K&R book myself when I learned C, what was sometime around the mid-80s though. But since AFAIK the ANSI standard wasn't settled before '89 it probably was the 1st edition if the 2nd one already had been "ANSIfied". I still recall that creepy K&R syntax for the declaration of function parameter lists...

Moving to managed C++ to work with forms has been a chore, but the help I get on this site has made it much easier. But, I have to ask one last question: A lot of the developers that I work with are telling me to give up on managed C++ and move directly into C#. So, are they correct? Is C++ a dying language that is being replaced by C#? Should I abandon my current path and learn the C# syntax?

Thanks again for all your help. Plan A is to stick with C++, but I could be swayed by someone who knows BOTH languages.

Actually, the vast majority of arrived developers aroud here will give you the same recommendation, and even I understand why they do so. My own preference for C++/CLI is definitely anti-mainstream. C# definitely does have its advantages like the integration of extension methods or the LINQ query definition syntax. While extension methods can pretty much be used in C++/CLI as well once you found out how that is done (and that's somewhat hard to find on MSDN - IIRC I, myself, found the simple trick in a blog post, more accidentally than not), LINQ queries are practically impossible in C++/CLI. The underlying .NET infrastructure is available and allows to do some fancy tricks, but the query definition syntax is not. That way writing LINQ queries in C++/CLI and C# is worlds apart and C# wins hands-down in this discipline.

The mainstream opinion about C++/CLI is that its only real use is managed/native interop. But while I agree that this in fact is C++/CLIs dominant and unique strength, IMO it is a viable general-purpose language as well.

One thing, for instance, I don't like that much about C# is that it tends to obscure more what's actually going on unter the hood than C++/CLI. E.g., its syntax for using value types vs. reference types is mostly identical, with the exception of their definition (value types are called struct there and reference types class) and the fact that reference types need to be instantiated with new. In C++/CLI, OTOH, the distinction between the two is syntactically ubiquotious and impossible to overlook (with the exception of implicitly dereferenced variables which, though, seems to be a hardly known and rarely used feature anyway).

Many HLL programmers nowadays seem to be perfectly comfortable with not knowing the low-level details, however, assembly language guys like you and me are somewhat likely to view that differently...

I can't really claim I know both languages. I'm just able to half-way decently read C# which is almost mandatory because lots of code samples simply aren't available in C++/CLI. I doubt I'm able to half-way fluently write C#, though.

Perhaps you'll get some more rather mainstream comments on this thread as well. The guys who really do know both languages or at least know C++/CLI about as good as I know C# come around here from time to time too (and I definitely appreciate that ). They're just less regular here than I am.

Last edited by Eri523; January 5th, 2012 at 05:24 PM.

I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.

This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.