Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

How do I pass argv to a class

Posted on 2001-06-20
4
Medium Priority
?
489 Views
Last Modified: 2010-04-02
I want to be able to pass the argv parameter given within the main() function of an app to a class, and assign it to a class member, such as :-

// from main

main(int argc, char* argv[])
{
   CMyClass myClass;

   myClass.Init(argc, argv);

......

}

// in myClass

CMyClass::Init(int argc, char* argv[])
{
   m_nArgc = argc;

  m_szArgv = argv;     // is this correct;

}

What I would like to know, is what data type must m_szArgv be declared as and how should argv be assigned to it?
0
Comment
Question by:carlpaddick
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 8

Accepted Solution

by:
mnashadka earned 200 total points
ID: 6209896
m_szArgv should be a char **, and the way you assign it is fine.  If you don't want to mess with char **'s, you might want to look at assigning argv to a vector of strings, like:

class CMyClass
{
...
  std::vector<std::string> m_Argv; // Could also be CString
};

// in myClass

CMyClass::Init(int argc, char* argv[])
{
  for(int i = 0; i < argc; ++i)
    m_Argv.push_back(argv[i]);
}
And you can still use the subscript operator to get data out later.  e.g. std::string param1 = m_Argv[0];

I hope this helps you.
0
 

Author Comment

by:carlpaddick
ID: 6209938
Thanks mnashadka,

Good answer.  Please have the points.

Keep up the good work.
0
 
LVL 22

Expert Comment

by:nietod
ID: 6222886
Does anyone know if this accepted answer was posted as a comment or as an answer?
0
 

Author Comment

by:carlpaddick
ID: 6223717
nietod,

Again, I posted this comment as an accepted answer.

Thanks,

Carl
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
Suggested Courses

636 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question