Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Apr 24th, 2016, 12:05 PM   #1
Smjprogrammer
Learning Very Basic Stuff
 
Smjprogrammer's Avatar
 
Join Date: Jan 2010
Location: United States
Posts: 978
Rep Power: 8 Smjprogrammer is on a distinguished road
#define and #undef in Class

I cannot find a definitive answer on weather or not it is a good idea to use #define in the private part of a class, and #undef in that class's destructor.

c++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. #include <Windows.h>
  3. using std::cout;
  4.  
  5. class My_Class
  6. {
  7. private:
  8. #define t TEXT
  9. HANDLE h = GetStdHandle(STD_OUTPUT_HANDLE);
  10. public:
  11. My_Class()
  12. {
  13. SetConsoleTitle(t("C/C++ Command Prompt"));
  14. SetConsoleTextAttribute(h, 0x0A);
  15. }
  16.  
  17. void output_test()
  18. {
  19. cout << "Hello, world!\n";
  20. }
  21.  
  22. ~My_Class()
  23. {
  24. #undef t
  25. }
  26. };
  27.  
  28. int main()
  29. {
  30. My_Class use;
  31. use.output_test();
  32. }// main.cpp
__________________
HP Laptop / AMD A4 - 3305 APU with Radeon HD Graphics 1.90 Ghz
Windows 10 64-Bit
Visual Studio Community 2015
Recommended Books - C++ All-In-One For Dummies / Webster's Computer Dictionary
Smjprogrammer is offline   Reply With Quote
Old May 2nd, 2016, 11:33 AM   #2
Smjprogrammer
Learning Very Basic Stuff
 
Smjprogrammer's Avatar
 
Join Date: Jan 2010
Location: United States
Posts: 978
Rep Power: 8 Smjprogrammer is on a distinguished road
Re: #define and #undef in Class

c++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. #include <Windows.h>
  3. using std::cout;
  4.  
  5. class My_Class
  6. {
  7. private:
  8. #define t TEXT
  9. #define s STD_OUTPUT_HANDLE
  10. #define title SetConsoleTitle(t("C/C++ Command Prompt"));
  11. HANDLE h = GetStdHandle(s);
  12. public:
  13. My_Class()
  14. {
  15. cout << title;
  16. SetConsoleTextAttribute(h, 0x0a);
  17. }
  18.  
  19. void output_test()
  20. {
  21. cout << "\bHello, world!\n";
  22. /*
  23. Ever since I started storing the title in the macro the
  24. first line of any cout has a integer in it. This is easily
  25. removed with \b. I would like to know why, and if there is
  26. a better way? Also is it valid practice to use the
  27. title in the fashion? Still looking for an answer to the original
  28. question.
  29. */
  30. }
  31.  
  32. ~My_Class()
  33. {
  34. #undef t
  35. #undef s
  36. #undef title
  37. }
  38. };
  39.  
  40. int main()
  41. {
  42. My_Class use;
  43. use.output_test();
  44. }// main.cpp
__________________
HP Laptop / AMD A4 - 3305 APU with Radeon HD Graphics 1.90 Ghz
Windows 10 64-Bit
Visual Studio Community 2015
Recommended Books - C++ All-In-One For Dummies / Webster's Computer Dictionary
Smjprogrammer is offline   Reply With Quote
Old May 6th, 2016, 1:06 PM   #3
Smjprogrammer
Learning Very Basic Stuff
 
Smjprogrammer's Avatar
 
Join Date: Jan 2010
Location: United States
Posts: 978
Rep Power: 8 Smjprogrammer is on a distinguished road
Re: #define and #undef in Class

Ignore post #2.

Ever since I started using cout to print macros and windows functions I get integers in my first command prompt line. This is easily removed with two of these \b. I would like to know why, and if there is a better way? Also is it valid practice to use the title and color in the fashion? Still looking for an answer to the original question.

c++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. #include <Windows.h>
  3. using std::cout;
  4.  
  5. class My_Class
  6. {
  7. private:
  8. #define t TEXT
  9. #define s STD_OUTPUT_HANDLE
  10. #define title SetConsoleTitle(t("C/C++ Command Prompt"));
  11. HANDLE h = GetStdHandle(s);
  12. BOOL color = SetConsoleTextAttribute(h, 0x0a);
  13. public:
  14. My_Class()
  15. {
  16. cout << title;
  17. cout << color;
  18. }
  19.  
  20. void output_test()
  21. {
  22. cout << "\b\bHello, world!\n";
  23. }
  24.  
  25. ~My_Class()
  26. {
  27. #undef t
  28. #undef s
  29. #undef title
  30. }
  31. };
  32.  
  33. int main()
  34. {
  35. My_Class use;
  36. use.output_test();
  37. }// main.cpp
__________________
HP Laptop / AMD A4 - 3305 APU with Radeon HD Graphics 1.90 Ghz
Windows 10 64-Bit
Visual Studio Community 2015
Recommended Books - C++ All-In-One For Dummies / Webster's Computer Dictionary
Smjprogrammer is offline   Reply With Quote
Old May 15th, 2016, 6:50 PM   #4
Smjprogrammer
Learning Very Basic Stuff
 
Smjprogrammer's Avatar
 
Join Date: Jan 2010
Location: United States
Posts: 978
Rep Power: 8 Smjprogrammer is on a distinguished road
Re: #define and #undef in Class

I have found a way to cleanly rid the integers/binary from the printed text.

c++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. #include <windows.h>
  3. using std::cout;
  4.  
  5. class My_Class
  6. {
  7. private:
  8. #define t TEXT
  9. #define s STD_OUTPUT_HANDLE
  10. #define title SetConsoleTitle (t("C/C++ Command Prompt"));
  11. HANDLE h = GetStdHandle (s);
  12. BOOL color = SetConsoleTextAttribute (h, 0x0A);
  13. char backspace[3] = "\b\b";
  14. public:
  15. My_Class()
  16. {
  17. cout << title;
  18. cout << color << backspace;
  19. }
  20.  
  21. void output_test()
  22. {
  23. cout << "Hello, world!\n";
  24. }
  25.  
  26. ~My_Class()
  27. {
  28. #undef t
  29. #undef s
  30. #undef title
  31. }
  32. };
  33.  
  34. int main()
  35. {
  36. My_Class use;
  37. use.output_test();
  38. }// main.cpp
__________________
HP Laptop / AMD A4 - 3305 APU with Radeon HD Graphics 1.90 Ghz
Windows 10 64-Bit
Visual Studio Community 2015
Recommended Books - C++ All-In-One For Dummies / Webster's Computer Dictionary
Smjprogrammer is offline   Reply With Quote
Old May 17th, 2016, 11:52 AM   #5
Smjprogrammer
Learning Very Basic Stuff
 
Smjprogrammer's Avatar
 
Join Date: Jan 2010
Location: United States
Posts: 978
Rep Power: 8 Smjprogrammer is on a distinguished road
Re: #define and #undef in Class

Ok so I spoke to soon. It works cleanly as long as you are not using random number.
__________________
HP Laptop / AMD A4 - 3305 APU with Radeon HD Graphics 1.90 Ghz
Windows 10 64-Bit
Visual Studio Community 2015
Recommended Books - C++ All-In-One For Dummies / Webster's Computer Dictionary
Smjprogrammer is offline   Reply With Quote
Old May 21st, 2016, 11:04 AM   #6
Smjprogrammer
Learning Very Basic Stuff
 
Smjprogrammer's Avatar
 
Join Date: Jan 2010
Location: United States
Posts: 978
Rep Power: 8 Smjprogrammer is on a distinguished road
Re: #define and #undef in Class

I have come to the conclusion that a more traditional approach is more likely the right one. I would still like to know somebody thinks about it all.

c++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. #include <windows.h>
  3. using std::cout;
  4.  
  5. #define t TEXT
  6. #define s STD_OUTPUT_HANDLE
  7. #define title SetConsoleTitle (t("C/C++ Command Prompt"));
  8.  
  9. class My_Class
  10. {
  11. private:
  12. HANDLE h = GetStdHandle (s);
  13. public:
  14. My_Class()
  15. {
  16. cout << title;// Is this a good idea.
  17. SetConsoleTextAttribute (h, 0x0A);
  18. cout << '\r';// Why does it need this.
  19. }
  20.  
  21. void output_test()
  22. {
  23. cout << "Hello, world!\n";
  24. }
  25. };
  26.  
  27. int main()
  28. {
  29. My_Class use;
  30. use.output_test();
  31. }// main.cpp
  32.  
  33. #undef t
  34. #undef s
  35. #undef title
__________________
HP Laptop / AMD A4 - 3305 APU with Radeon HD Graphics 1.90 Ghz
Windows 10 64-Bit
Visual Studio Community 2015
Recommended Books - C++ All-In-One For Dummies / Webster's Computer Dictionary
Smjprogrammer is offline   Reply With Quote
Old Jun 5th, 2016, 9:27 PM   #7
Smjprogrammer
Learning Very Basic Stuff
 
Smjprogrammer's Avatar
 
Join Date: Jan 2010
Location: United States
Posts: 978
Rep Power: 8 Smjprogrammer is on a distinguished road
Re: #define and #undef in Class

I have nothing else to program at the moment because I would like an answer from somebody about the above code. I refuse to move on until the enigma is solved. Things like this drive me nuts. Sure I know traffic is almost nonexistent on this forum these days, but I see that there are hundreds of views of this thread. This makes it hard for me to belief that not one single person has this or that to say.
__________________
HP Laptop / AMD A4 - 3305 APU with Radeon HD Graphics 1.90 Ghz
Windows 10 64-Bit
Visual Studio Community 2015
Recommended Books - C++ All-In-One For Dummies / Webster's Computer Dictionary
Smjprogrammer is offline   Reply With Quote
Old Jun 15th, 2016, 7:23 PM   #8
FlakkenTime
Software Developer
 
FlakkenTime's Avatar
 
Join Date: Apr 2010
Location: Bay Area, CA
Posts: 268
Rep Power: 8 FlakkenTime is on a distinguished road
Re: #define and #undef in Class

Quote:
Sure I know traffic is almost nonexistent on this forum these days, but I see that there are hundreds of views of this thread. This makes it hard for me to belief that not one single person has this or that to say.
Sad but true. I came back a year or so ago and noticed everything was just dead so I didn't come back. Suddenly thought I'd check it out while I was looking something up.

Anyhow. Those hundreds of views are likely just bots scrapping pages. They follow every link thus giving fake views.

Sadly i'm not a C programmer so I'm no help there.
FlakkenTime is offline   Reply With Quote
Old Jun 16th, 2016, 10:13 AM   #9
Smjprogrammer
Learning Very Basic Stuff
 
Smjprogrammer's Avatar
 
Join Date: Jan 2010
Location: United States
Posts: 978
Rep Power: 8 Smjprogrammer is on a distinguished road
Re: #define and #undef in Class

Bots yep I wonder about that a few times. Some of the names around here are pretty fake. If anyone who is human and knows C or C++ for the love of programming.
__________________
HP Laptop / AMD A4 - 3305 APU with Radeon HD Graphics 1.90 Ghz
Windows 10 64-Bit
Visual Studio Community 2015
Recommended Books - C++ All-In-One For Dummies / Webster's Computer Dictionary
Smjprogrammer is offline   Reply With Quote
Old Jun 16th, 2016, 5:19 PM   #10
FlakkenTime
Software Developer
 
FlakkenTime's Avatar
 
Join Date: Apr 2010
Location: Bay Area, CA
Posts: 268
Rep Power: 8 FlakkenTime is on a distinguished road
Re: #define and #undef in Class

Asked a friend of mine. Said its largely just a matter of personal opinion and coding practices. There doesn't seem to be a standard on it.

Id suggest doing your own style and stick with it or follow the format of the place you work.
FlakkenTime is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
undeclared (first use this function) ? RareDevil C++ 3 Oct 16th, 2008 9:35 PM
Defining a bool type nnxion C 6 Oct 7th, 2005 9:39 AM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 11:23 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC