473,322 Members | 1,734 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

Static classes like C#

Is there a way to implement static classes in C++ the same way you do in C#?

static class CompanyInfo
{
public static string GetCompanyName() { return "CompanyName"; }
public static string GetCompanyAddress() { return "CompanyAddress"; }
//...
}

May 18 '06 #1
4 1066
"Brian Richards" <br*******@gmail.com> wrote in message
news:Ow**************@TK2MSFTNGP03.phx.gbl...
Is there a way to implement static classes in C++ the same way you do in
C#?


Sure. Just leave off the "static" keyword" on the class definition :)

Seriously though - there's no C++ equivalent to a class that CAN'T have
instance methods - but you can just make a class and only create static
methods in it. For completeness, declare an unimplemented, private default
constructor so the class cannot be instantiated.

-cd

May 18 '06 #2
Carl Daniel [VC++ MVP] wrote:
"Brian Richards" <br*******@gmail.com> wrote in message
news:Ow**************@TK2MSFTNGP03.phx.gbl...
Is there a way to implement static classes in C++ the same way you
do in C#?


Sure. Just leave off the "static" keyword" on the class definition :)

Seriously though - there's no C++ equivalent to a class that CAN'T
have instance methods


free functions in a namespace?

Jeff Flinn
May 18 '06 #3
Free functions is how I had it before but I wanted to share the methods with
a C# assembly so I was refactoring to a class.

Thanks

"Jeff F" <no*@anywhere.com> wrote in message
news:%2****************@TK2MSFTNGP02.phx.gbl...
Carl Daniel [VC++ MVP] wrote:
"Brian Richards" <br*******@gmail.com> wrote in message
news:Ow**************@TK2MSFTNGP03.phx.gbl...
Is there a way to implement static classes in C++ the same way you
do in C#?


Sure. Just leave off the "static" keyword" on the class definition :)

Seriously though - there's no C++ equivalent to a class that CAN'T
have instance methods


free functions in a namespace?

Jeff Flinn

May 18 '06 #4
"Brian Richards" wrote:
Is there a way to implement static classes in C++ the same way you do in C#?

static class CompanyInfo
{
public static string GetCompanyName() { return "CompanyName"; }
public static string GetCompanyAddress() { return "CompanyAddress"; }
//...
}


If you're using C++/CLI, declare a class to be both abstract and sealed:

#using <System.dll>

public ref class CompanyInfo abstract sealed
{
private:
CompanyInfo();

public:
static String^ GetCompanyName() {...}
static String^ GetCompanyAddress() {...}
...
}

Sean
May 19 '06 #5

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

Similar topics

5
by: Thomas Matthews | last post by:
Hi, I have three classes: Category Author Publisher Each of these classes stores its information into a database table of <ID, text>. (They are fields that have a foreign key.) There is...
9
by: Laban | last post by:
Hi, I find myself using static methods more than I probably should, so I am looking for some advice on a better approach. For example, I am writing an app that involves quite a bit of database...
4
by: Bryan Green | last post by:
So I'm working on a project for a C# class I'm taking, where I need to keep some running totals via static variables. I need three classes for three different types of objects. The base class and...
11
by: Kevin Prichard | last post by:
Hi all, I've recently been following the object-oriented techiques discussed here and have been testing them for use in a web application. There is problem that I'd like to discuss with you...
6
by: MSDNAndi | last post by:
Hi, I have a baseclass (non-static) with some static and some non-static methods/fields/properties. In the baseclass in one of the static methods I need to do something like " somelogic...
4
by: DBC User | last post by:
I have a class with bunch of static methods. I could regourp all the static methods into seperate 3 or 4 classes. I was thinking about using Singlton pattern for all these 4 classes so that it...
6
by: GroupReader | last post by:
In my app, I have two very similar static classes. After long thought, I've decided *yes - keep them static*. - Sometimes I will want to use Static Class A, and somtimes I will want to use...
2
by: mark4asp | last post by:
Q: Initialising and updating a class with only static members & database dependency I have a class with the following members: public static List<ACISACIS_List; static AssetClass() { //...
17
by: Juha Nieminen | last post by:
As we know, the keyword "inline" is a bit misleading because its meaning has changed in practice. In most modern compilers it has completely lost its meaning of "a hint for the compiler to inline...
9
by: puzzlecracker | last post by:
It looks like static classes can have non-static member classes -- does it make sense what I just wrote? If not, let me illustrate it: public static class Foo{ private struct Bar{ } }
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.